This is an automated email from the ASF dual-hosted git repository.

lkishalmi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 18ef974e3ba Fix Yaml Editor does not respect indent size on new line
18ef974e3ba is described below

commit 18ef974e3ba29e0e5f23c8d33a23ef07026397c5
Author: Laszlo Kishalmi <[email protected]>
AuthorDate: Sat Jul 13 16:10:13 2024 -0700

    Fix Yaml Editor does not respect indent size on new line
---
 ide/languages.yaml/nbproject/project.properties    |  2 +-
 .../yaml/EmbeddedSectionsHighlighting.java         | 12 ++---
 .../modules/languages/yaml/IndentUtils.java        | 52 ----------------------
 .../modules/languages/yaml/InsertTabAction.java    | 20 +++++----
 .../languages/yaml/YamlKeystrokeHandler.java       | 14 ++++--
 5 files changed, 29 insertions(+), 71 deletions(-)

diff --git a/ide/languages.yaml/nbproject/project.properties 
b/ide/languages.yaml/nbproject/project.properties
index a3b66e2705a..573c106751a 100644
--- a/ide/languages.yaml/nbproject/project.properties
+++ b/ide/languages.yaml/nbproject/project.properties
@@ -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
diff --git 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/EmbeddedSectionsHighlighting.java
 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/EmbeddedSectionsHighlighting.java
index 518201974a6..b01e5e91a44 100644
--- 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/EmbeddedSectionsHighlighting.java
+++ 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/EmbeddedSectionsHighlighting.java
@@ -26,6 +26,7 @@ import javax.swing.text.AttributeSet;
 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;
@@ -35,7 +36,6 @@ import org.netbeans.api.lexer.TokenHierarchyEvent;
 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;
@@ -171,13 +171,13 @@ public class EmbeddedSectionsHighlighting extends 
AbstractHighlightsContainer im
 
                             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
                                     {
@@ -185,10 +185,10 @@ public class EmbeddedSectionsHighlighting extends 
AbstractHighlightsContainer im
                                     }
 
                                     // 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;
                                     }
diff --git 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/IndentUtils.java 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/IndentUtils.java
deleted file mode 100644
index 27c5bb2a83e..00000000000
--- 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/IndentUtils.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.netbeans.modules.languages.yaml;
-
-import java.util.prefs.Preferences;
-import javax.swing.text.Document;
-import org.netbeans.api.editor.settings.SimpleValueNames;
-import org.netbeans.modules.editor.indent.spi.CodeStylePreferences;
-
-/**
- *
- * @author Ondrej Brejla <[email protected]>
- */
-public class IndentUtils {
-
-    private IndentUtils() {
-    }
-
-    public static int getIndentSize(Document doc) {
-        Preferences prefs = CodeStylePreferences.get(doc).getPreferences();
-        return prefs.getInt(SimpleValueNames.SPACES_PER_TAB, 2);
-    }
-
-    private static void indent(final StringBuilder sb, final int indent) {
-        for (int i = 0; i < indent; i++) {
-            sb.append(' ');
-        }
-    }
-
-    public static String getIndentString(final int indent) {
-        StringBuilder sb = new StringBuilder(indent);
-        indent(sb, indent);
-        return sb.toString();
-    }
-
-}
diff --git 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/InsertTabAction.java
 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/InsertTabAction.java
index e2272ff9b44..2e84ffb38f5 100644
--- 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/InsertTabAction.java
+++ 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/InsertTabAction.java
@@ -19,18 +19,21 @@
 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;
 
 /**
  *
@@ -38,11 +41,7 @@ import org.netbeans.editor.Utilities;
  */
 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);
@@ -91,11 +90,11 @@ public class InsertTabAction extends BaseAction {
         }
 
         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);
             }
         }
@@ -106,6 +105,11 @@ public class InsertTabAction extends BaseAction {
 
     }
 
+    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;
     }
diff --git 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlKeystrokeHandler.java
 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlKeystrokeHandler.java
index a6c1e8f2881..4005df2c73f 100644
--- 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlKeystrokeHandler.java
+++ 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlKeystrokeHandler.java
@@ -26,6 +26,7 @@ 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.api.lexer.Token;
 import org.netbeans.api.lexer.TokenHierarchy;
 import org.netbeans.api.lexer.TokenSequence;
@@ -37,6 +38,7 @@ import org.netbeans.modules.csl.api.KeystrokeHandler;
 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;
 
 /**
@@ -226,7 +228,6 @@ public class YamlKeystrokeHandler implements 
KeystrokeHandler {
 
     @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
@@ -289,9 +290,9 @@ public class YamlKeystrokeHandler implements 
KeystrokeHandler {
         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
         }
@@ -310,7 +311,7 @@ public class YamlKeystrokeHandler implements 
KeystrokeHandler {
         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);
@@ -386,4 +387,9 @@ public class YamlKeystrokeHandler implements 
KeystrokeHandler {
         }
     }
 
+    private static int getIndentSize(Document doc) {
+        return CodeStylePreferences.get(doc).getPreferences()
+                .getInt(SimpleValueNames.INDENT_SHIFT_WIDTH, 2);
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to