Skip to content

Commit

Permalink
Fix Yaml Editor does not respect indent size on new line
Browse files Browse the repository at this point in the history
  • Loading branch information
lkishalmi committed Jul 18, 2024
1 parent 31a270c commit 18ef974
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 71 deletions.
2 changes: 1 addition & 1 deletion ide/languages.yaml/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
javac.source=1.8
javac.release=17

test-unit-sys-prop.xtest.jruby.home=${netbeans.dest.dir}/ruby/jruby-1.3.1
test-unit-sys-prop.xtest.platform_info.rb=${netbeans.dest.dir}/ruby/platform_info.rb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.StyleConstants;
import org.netbeans.api.editor.document.LineDocumentUtils;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.editor.mimelookup.MimeRegistration;
import org.netbeans.api.editor.settings.AttributesUtilities;
Expand All @@ -35,7 +36,6 @@
import org.netbeans.api.lexer.TokenHierarchyListener;
import org.netbeans.api.lexer.TokenSequence;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.Utilities;
import org.netbeans.lib.editor.util.swing.DocumentUtilities;
import static org.netbeans.modules.languages.yaml.YamlLanguage.MIME_TYPE;
import org.netbeans.spi.editor.highlighting.HighlightsLayer;
Expand Down Expand Up @@ -171,24 +171,24 @@ public boolean moveNext() {

try {
int docLen = document.getLength();
int startLine = Utilities.getLineOffset((BaseDocument) document, Math.min(sectionStart, docLen));
int endLine = Utilities.getLineOffset((BaseDocument) document, Math.min(sectionEnd, docLen));
int startLine = LineDocumentUtils.getLineIndex((BaseDocument) document, Math.min(sectionStart, docLen));
int endLine = LineDocumentUtils.getLineIndex((BaseDocument) document, Math.min(sectionEnd, docLen));

if (startLine != endLine) {
// multiline scriplet section
// adjust the sections start to the beginning of the firts line
int firstLineStartOffset = Utilities.getRowStartFromLineOffset((BaseDocument) document, startLine);
int firstLineStartOffset = LineDocumentUtils.getLineStart((BaseDocument)document, startLine);
if (firstLineStartOffset < sectionStart - delimiterSize
&& isWhitespace(document, firstLineStartOffset, sectionStart - delimiterSize)) // always preceeded by the delimiter
{
sectionStart = firstLineStartOffset;
}

// adjust the sections end to the end of the last line
int lines = Utilities.getRowCount((BaseDocument) document);
int lines = LineDocumentUtils.getLineCount((BaseDocument) document);
int lastLineEndOffset;
if (endLine + 1 < lines) {
lastLineEndOffset = Utilities.getRowStartFromLineOffset((BaseDocument) document, endLine + 1);
lastLineEndOffset = LineDocumentUtils.getLineStartFromIndex((BaseDocument) document, endLine + 1);
} else {
lastLineEndOffset = document.getLength() + 1;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,29 @@
package org.netbeans.modules.languages.yaml;

import java.awt.event.ActionEvent;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.api.editor.document.LineDocumentUtils;
import org.netbeans.api.editor.settings.SimpleValueNames;
import org.netbeans.editor.BaseAction;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.BaseKit;
import org.netbeans.editor.Utilities;
import org.netbeans.modules.editor.indent.spi.CodeStylePreferences;

/**
*
* @author Ondrej Brejla <[email protected]>
*/
public class InsertTabAction extends BaseAction {

private static final List<Action> CUSTOM_ACTIONS = new LinkedList<Action>();

static {
CUSTOM_ACTIONS.add(new InsertTabAction());
}
private static final List<Action> CUSTOM_ACTIONS = List.of(new InsertTabAction());

public InsertTabAction() {
super(BaseKit.insertTabAction);
Expand Down Expand Up @@ -91,11 +90,11 @@ private void tryReplaceTab() throws BadLocationException {
}

private void replaceTab() throws BadLocationException {
final int rowStart = Utilities.getRowStart(baseDocument, caretOffset);
final int rowStart = LineDocumentUtils.getLineStart(baseDocument, caretOffset);
assert caretOffset >= rowStart : "Caret: " + caretOffset + " rowStart: " + rowStart;
final String indentString = baseDocument.getText(rowStart, caretOffset - rowStart);
if (indentString.contains(TAB_CHARACTER)) {
final String newIndentString = indentString.replace(TAB_CHARACTER, IndentUtils.getIndentString(IndentUtils.getIndentSize(baseDocument)));
final String newIndentString = indentString.replace(TAB_CHARACTER, " ".repeat(getSpacesPerTab(baseDocument)));
baseDocument.replace(rowStart, caretOffset - rowStart, newIndentString, null);
}
}
Expand All @@ -106,6 +105,11 @@ private static boolean shouldBeReplaced(final int firstNonWhiteCharOffset, final

}

private static int getSpacesPerTab(Document doc) {
return CodeStylePreferences.get(doc).getPreferences()
.getInt(SimpleValueNames.SPACES_PER_TAB, 2);
}

public static List<Action> createCustomActions() {
return CUSTOM_ACTIONS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.api.editor.document.LineDocumentUtils;
import org.netbeans.api.editor.settings.SimpleValueNames;
import org.netbeans.api.lexer.Token;
import org.netbeans.api.lexer.TokenHierarchy;
import org.netbeans.api.lexer.TokenSequence;
Expand All @@ -37,6 +38,7 @@
import org.netbeans.modules.csl.api.OffsetRange;
import org.netbeans.modules.csl.api.StructureItem;
import org.netbeans.modules.csl.spi.ParserResult;
import org.netbeans.modules.editor.indent.spi.CodeStylePreferences;
import org.openide.util.Exceptions;

/**
Expand Down Expand Up @@ -226,7 +228,6 @@ public boolean afterCharInserted(Document doc, int caretOffset, JTextComponent t

@Override
public boolean charBackspaced(Document doc, int dotPos, JTextComponent target, char ch) throws BadLocationException {
Caret caret = target.getCaret();
if (ch == '%' && dotPos > 0 && dotPos <= doc.getLength() - 2) {
String s = doc.getText(dotPos - 1, 3);
if ("<%>".equals(s)) { // NOI18N
Expand Down Expand Up @@ -289,9 +290,9 @@ public int beforeBreak(Document document, int offset, JTextComponent target) thr
int indent = getLineIndent(doc, offset);
String linePrefix = doc.getText(lineBegin, offset - lineBegin);
String lineSuffix = doc.getText(offset, lineEnd + 1 - offset);
if (linePrefix.trim().endsWith(":") && lineSuffix.trim().length() == 0) {
if (linePrefix.stripTrailing().endsWith(":") && lineSuffix.isBlank()) {
// Yes, new key: increase indent
indent += IndentUtils.getIndentSize(doc);
indent += getIndentSize(doc);
} else {
// No, just use same indent as parent
}
Expand All @@ -310,7 +311,7 @@ public int beforeBreak(Document document, int offset, JTextComponent target) thr
if (remove > 0) {
doc.remove(offset, remove);
}
String str = IndentUtils.getIndentString(indent);
String str = " ".repeat(indent);
int newPos = offset + str.length();
doc.insertString(offset, str, null);
caret.setDot(offset);
Expand Down Expand Up @@ -386,4 +387,9 @@ public static int getLineIndent(BaseDocument doc, int offset) {
}
}

private static int getIndentSize(Document doc) {
return CodeStylePreferences.get(doc).getPreferences()
.getInt(SimpleValueNames.INDENT_SHIFT_WIDTH, 2);
}

}

0 comments on commit 18ef974

Please sign in to comment.