You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/07/17 11:13:17 UTC

[GitHub] junichi11 closed pull request #550: [NETBEANS-791] Highlights for the top line of JavaDoc are done on every line.

junichi11 closed pull request #550: [NETBEANS-791] Highlights for the top line of JavaDoc are done on every line.
URL: https://github.com/apache/incubator-netbeans/pull/550
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/javadoc/nbproject/project.properties b/javadoc/nbproject/project.properties
index 55faa449a..853336e74 100644
--- a/javadoc/nbproject/project.properties
+++ b/javadoc/nbproject/project.properties
@@ -16,7 +16,7 @@
 # under the License.
 
 javac.compilerargs=-Xlint:unchecked
-javac.source=1.7
+javac.source=1.8
 
 # requires nb.javac for compiling of tests on Mac
 requires.nb.javac=true
diff --git a/javadoc/src/org/netbeans/modules/javadoc/highlighting/Highlighting.java b/javadoc/src/org/netbeans/modules/javadoc/highlighting/Highlighting.java
index 2f1cfb9aa..d022a8f55 100644
--- a/javadoc/src/org/netbeans/modules/javadoc/highlighting/Highlighting.java
+++ b/javadoc/src/org/netbeans/modules/javadoc/highlighting/Highlighting.java
@@ -20,6 +20,7 @@
 package org.netbeans.modules.javadoc.highlighting;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.ConcurrentModificationException;
 import java.util.Enumeration;
@@ -45,6 +46,7 @@
 import org.netbeans.api.lexer.TokenHierarchyListener;
 import org.netbeans.api.lexer.TokenId;
 import org.netbeans.api.lexer.TokenSequence;
+import org.netbeans.api.lexer.TokenUtilities;
 import org.netbeans.spi.editor.highlighting.HighlightsSequence;
 import org.netbeans.spi.editor.highlighting.support.AbstractHighlightsContainer;
 import org.openide.util.WeakListeners;
@@ -55,21 +57,26 @@
 public class Highlighting extends AbstractHighlightsContainer implements TokenHierarchyListener {
 
     private static final Logger LOG = Logger.getLogger(Highlighting.class.getName());
-    
+    private static final String WS = " \t\n"; // NOI18N
+    private static final String JAPANESE_PERIOD = "\u3002"; // 。 NOI18N
+    private static final List<String> PERIODS = Arrays.asList(
+            JAPANESE_PERIOD
+    );
+
     public static final String LAYER_ID = "org.netbeans.modules.javadoc.highlighting"; //NOI18N
-    
+
     private final AttributeSet fontColor;
-    
+
     private final Document document;
     private TokenHierarchy<? extends Document> hierarchy = null;
     private final AtomicLong version = new AtomicLong();
-    
+
     /** Creates a new instance of Highlighting */
     public Highlighting(Document doc) {
         AttributeSet firstLineFontColor = MimeLookup.getLookup(MimePath.get("text/x-java")).lookup(FontColorSettings.class).getTokenFontColors("javadoc-first-sentence"); //NOI18N
         AttributeSet commentFontColor = MimeLookup.getLookup(MimePath.get("text/x-java")).lookup(FontColorSettings.class).getTokenFontColors("comment"); //NOI18N
         if(firstLineFontColor != null && commentFontColor != null) {
-            Collection<Object> attrs = new LinkedList<Object>();
+            Collection<Object> attrs = new LinkedList<>();
             for (Enumeration<?> e = firstLineFontColor.getAttributeNames(); e.hasMoreElements(); ) {
                 Object key = e.nextElement();
                 Object value = firstLineFontColor.getAttribute(key);
@@ -110,7 +117,7 @@ public HighlightsSequence getHighlights(int startOffset, int endOffset) {
     public void tokenHierarchyChanged(TokenHierarchyEvent evt) {
         TokenChange<?> tc = evt.tokenChange();
         int affectedArea [] = null;
-        
+
         TokenSequence<? extends TokenId> seq = tc.currentTokenSequence();
         if (seq.language().equals(JavadocTokenId.language())) {
             // Change inside javadoc
@@ -131,7 +138,7 @@ public void tokenHierarchyChanged(TokenHierarchyEvent evt) {
             // find out whether it really involves javadoc or not.
             affectedArea = new int [] { tc.offset(), evt.affectedEndOffset() };
         }
-        
+
         if (affectedArea != null) {
             version.incrementAndGet();
             fireHighlightsChange(affectedArea[0], affectedArea[1]);
@@ -147,6 +154,18 @@ public void tokenHierarchyChanged(TokenHierarchyEvent evt) {
         if (seq.moveNext()) {
             int start = seq.offset();
             do {
+                String period = null;
+                int indexOfPeriod = -1;
+                for (String p : PERIODS) {
+                    int index = TokenUtilities.indexOf(seq.token().text(), p);
+                    if (index != -1) {
+                        if (indexOfPeriod == -1 || index < indexOfPeriod) {
+                            indexOfPeriod = index;
+                            period = p;
+                        }
+                    }
+                }
+
                 if (seq.token().id() == JavadocTokenId.DOT) {
                     if (seq.moveNext()) {
                         if (isWhiteSpace(seq.token())) {
@@ -154,6 +173,15 @@ public void tokenHierarchyChanged(TokenHierarchyEvent evt) {
                         }
                         seq.movePrevious();
                      }
+                } else if (period != null && indexOfPeriod != -1) {
+                    // NETBEANS-791
+                    int offset = indexOfPeriod + 1;
+                    while (offset < seq.token().length()
+                            && isPeriod(seq.token().text().subSequence(offset, offset + 1))) {
+                        // e.g. 。。。
+                        offset++;
+                    }
+                    return new int[]{start, seq.offset() + offset};
                 } else if (seq.token().id() == JavadocTokenId.TAG) {
                     if (seq.movePrevious()) {
                         if (!seq.token().text().toString().trim().endsWith("{")) {
@@ -172,21 +200,24 @@ private static boolean isWhiteSpace(Token<? extends TokenId> token) {
         if (token == null || token.id() != JavadocTokenId.OTHER_TEXT) {
             return false;
         }
-        String ws = " \t\n";
-        return ws.indexOf(token.text().charAt(0)) >= 0;
+        return WS.indexOf(token.text().charAt(0)) >= 0;
+    }
+
+    private static boolean isPeriod(CharSequence cs) {
+        return PERIODS.stream().anyMatch(period -> TokenUtilities.equals(cs, period));
     }
 
     private final class HSImpl implements HighlightsSequence {
-        
-        private long version;
-        private TokenHierarchy<? extends Document> scanner;
+
+        private final long version;
+        private final TokenHierarchy<? extends Document> scanner;
         private List<TokenSequence<? extends TokenId>> sequences;
-        private int startOffset;
-        private int endOffset;
-        
+        private final int startOffset;
+        private final int endOffset;
+
         private List<Integer> lines = null;
         private int linesIdx = -1;
-        
+
         public HSImpl(long version, TokenHierarchy<? extends Document> scanner, int startOffset, int endOffset) {
             this.version = version;
             this.scanner = scanner;
@@ -195,10 +226,11 @@ public HSImpl(long version, TokenHierarchy<? extends Document> scanner, int star
             this.sequences = null;
         }
 
+        @Override
         public boolean moveNext() {
             synchronized (Highlighting.this) {
                 checkVersion();
-                
+
                 if (sequences == null) {
                     // initialize
                     TokenSequence<?> tokenSequence = scanner.tokenSequence();
@@ -208,7 +240,7 @@ public boolean moveNext() {
                         return false;
                     }
                     TokenSequence<?> seq = tokenSequence.subSequence(startOffset, endOffset);
-                    sequences = new ArrayList<TokenSequence<? extends TokenId>>();
+                    sequences = new ArrayList<>();
                     sequences.add(seq);
                 }
 
@@ -217,11 +249,11 @@ public boolean moveNext() {
                         linesIdx += 2;
                         return true;
                     }
-                    
+
                     lines = null;
                     linesIdx = -1;
                 }
-                
+
                 while (!sequences.isEmpty()) {
                     TokenSequence<? extends TokenId> seq = sequences.get(sequences.size() - 1);
 
@@ -257,10 +289,11 @@ public boolean moveNext() {
             }
         }
 
+        @Override
         public int getStartOffset() {
             synchronized (Highlighting.this) {
                 checkVersion();
-                
+
                 if (sequences == null) {
                     throw new NoSuchElementException("Call moveNext() first."); //NOI18N
                 }
@@ -273,10 +306,11 @@ public int getStartOffset() {
             }
         }
 
+        @Override
         public int getEndOffset() {
             synchronized (Highlighting.this) {
                 checkVersion();
-                
+
                 if (sequences == null) {
                     throw new NoSuchElementException("Call moveNext() first."); //NOI18N
                 }
@@ -289,10 +323,11 @@ public int getEndOffset() {
             }
         }
 
+        @Override
         public AttributeSet getAttributes() {
             synchronized (Highlighting.this) {
                 checkVersion();
-                
+
                 if (sequences == null) {
                     throw new NoSuchElementException("Call moveNext() first."); //NOI18N
                 }
@@ -304,17 +339,17 @@ public AttributeSet getAttributes() {
                 }
             }
         }
-        
+
         private void checkVersion() {
             if (this.version != Highlighting.this.version.get()) {
                 throw new ConcurrentModificationException();
             }
         }
-        
+
         private List<Integer> splitByLines(int sentenceStart, int sentenceEnd) {
-            ArrayList<Integer> lines = new ArrayList<Integer>();
+            ArrayList<Integer> lines = new ArrayList<>();
             int offset = sentenceStart;
-            
+
             try {
                 while (offset < sentenceEnd) {
                     Element lineElement = document.getDefaultRootElement().getElement(
@@ -325,9 +360,9 @@ private void checkVersion() {
 
                     String line = document.getText(rowStart, rowEnd - rowStart);
                     int idx = 0;
-                    while (idx < line.length() && 
-                        (line.charAt(idx) == ' ' || 
-                        line.charAt(idx) == '\t' || 
+                    while (idx < line.length() &&
+                        (line.charAt(idx) == ' ' ||
+                        line.charAt(idx) == '\t' ||
                         line.charAt(idx) == '*'))
                     {
                         idx++;
@@ -343,7 +378,7 @@ private void checkVersion() {
             } catch (BadLocationException e) {
                 LOG.log(Level.WARNING, "Can't determine javadoc first sentence", e);
             }
-            
+
             return lines.isEmpty() ? null : lines;
         }
     } // End of HSImpl class
diff --git a/javadoc/test/unit/src/org/netbeans/modules/javadoc/highlighting/HighlightingTest.java b/javadoc/test/unit/src/org/netbeans/modules/javadoc/highlighting/HighlightingTest.java
new file mode 100644
index 000000000..2e6ec5be6
--- /dev/null
+++ b/javadoc/test/unit/src/org/netbeans/modules/javadoc/highlighting/HighlightingTest.java
@@ -0,0 +1,209 @@
+/*
+ * 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.javadoc.highlighting;
+
+import javax.swing.text.AbstractDocument;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.DefaultStyledDocument;
+import javax.swing.text.Document;
+import javax.swing.text.SimpleAttributeSet;
+import static junit.framework.TestCase.fail;
+import org.netbeans.api.java.lexer.JavaTokenId;
+import org.netbeans.api.lexer.Language;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.spi.editor.highlighting.HighlightsSequence;
+
+public class HighlightingTest extends NbTestCase {
+
+    public HighlightingTest(String name) {
+        super(name);
+    }
+
+    public void testHighlights() throws Exception {
+        String content
+                = "/**the 1st line.\n"
+                + " *\n"
+                + " * the 3rd line\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 16});
+    }
+
+    public void testHighlightsWithMultiSentences_01() throws Exception {
+        String content
+                = "/**the 1st line. multiple sentences.\n"
+                + " *\n"
+                + " * the 3rd line.\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 16});
+    }
+
+    public void testHighlightsWithMultiSentences_02() throws Exception {
+        String content
+                = "/**the 1st line.multiple sentences.\n"
+                + " *\n"
+                + " * the 3rd line.\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 35});
+    }
+
+    public void testHighlightsOnMultiLines_01() throws Exception {
+        String content
+                = "/**\n"
+                + " * the 2nd line.\n"
+                + " * the 3rd line.\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 4, 7, 20});
+    }
+
+    public void testHighlightsOnMultiLines_02() throws Exception {
+        String content
+                = "/**the 1st line\n"
+                + " * the 2nd line.\n"
+                + " * the 3rd line.\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 16, 19, 32});
+    }
+
+    public void testHighlightsOnMultiLines_03() throws Exception {
+        String content
+                = "/**the first line\n"
+                + " *\n"
+                + " * the 3rd line.\n"
+                + " * the 4th line.\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 18, 20, 21, 24, 37});
+    }
+
+    public void testHighlightsWithMultiPeriods() throws Exception {
+        String content
+                = "/**the 1st line...\n"
+                + " *\n"
+                + " * the 3rd line.\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 18});
+    }
+
+    // Japanese
+    public void testHighlightsForJapanese() throws Exception {
+        String content
+                = "/**1行目。\n"
+                + " *\n"
+                + " * 3行目。\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 7});
+    }
+
+    public void testHighlightsForJapaneseWithMultiSentences() throws Exception {
+        String content
+                = "/**1行目。複数の文。\n"
+                + " *\n"
+                + " * 3行目。\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 7});
+    }
+
+    public void testHighlightsForJapaneseOnMultiLines_01() throws Exception {
+        String content
+                = "/**\n"
+                + " * 2行目。\n"
+                + " * 3行目。\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 4, 7, 11});
+    }
+
+    public void testHighlightsForJapaneseOnMultiLines_02() throws Exception {
+        String content
+                = "/**1行目\n"
+                + " * 2行目。\n"
+                + " * 3行目。\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 7, 10, 14});
+    }
+
+    public void testHighlightsForJapaneseOnMultiLines_03() throws Exception {
+        String content
+                = "/**1行目\n"
+                + " *\n"
+                + " * 3行目。\n"
+                + " * 4行目。\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 7, 9, 10, 13, 17});
+    }
+
+    public void testHighlightsForJapaneseWithMultiPeriods() throws Exception {
+        String content
+                = "/**1行目。。。\n"
+                + " *\n"
+                + " * 3行目。\n"
+                + " * @author junichi11\n"
+                + " */";
+        checkHighlights(content, new int[]{3, 9});
+    }
+
+    /**
+     * Check highlight ranges.
+     *
+     * @param content Javadoc
+     * @param offsetRanges start and end offset ranges
+     */
+    private void checkHighlights(String content, int[] offsetRanges) {
+        Document document = createDocument(content);
+        ((AbstractDocument) document).readLock();
+        try {
+            Highlighting highlighting = new Highlighting(document);
+            HighlightsSequence hs = highlighting.getHighlights(0, document.getLength());
+            assertEquals(0, offsetRanges.length % 2);
+            assertTrue(hs.moveNext());
+            for (int i = 0; i < offsetRanges.length; i += 2) {
+                assertEquals(offsetRanges[i], hs.getStartOffset());
+                assertEquals(offsetRanges[i + 1], hs.getEndOffset());
+                if (!hs.moveNext()) {
+                    assertEquals(offsetRanges.length, i + 2);
+                    break;
+                }
+            }
+        } finally {
+            ((AbstractDocument) document).readUnlock();
+        }
+    }
+
+    private Document createDocument(String text) {
+        try {
+            DefaultStyledDocument doc = new DefaultStyledDocument();
+            doc.putProperty(Language.class, JavaTokenId.language());
+            doc.insertString(0, text, SimpleAttributeSet.EMPTY);
+            return doc;
+        } catch (BadLocationException e) {
+            fail(e.getMessage());
+        }
+        return null;
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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