This is an automated email from the ASF dual-hosted git repository. akhileshsingh 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 f3a96e60a3 Add markup tag completion hints for inline snippets (#4107) f3a96e60a3 is described below commit f3a96e60a32910cb0f838ef7c83ca16d44c377d9 Author: Meghna Jayan <meghna.ja...@oracle.com> AuthorDate: Mon Jul 11 10:59:48 2022 +0530 Add markup tag completion hints for inline snippets (#4107) * add markup tag completion for snippets --- .../java/editor/javadoc/JavadocCompletionTask.java | 69 +++++++ .../editor/javadoc/JavaDocCompletionTaskTest.java | 215 +++++++++++++++++++++ 2 files changed, 284 insertions(+) diff --git a/java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionTask.java b/java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionTask.java index 7b5f00f569..7dfa41084e 100644 --- a/java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionTask.java +++ b/java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionTask.java @@ -31,6 +31,7 @@ import com.sun.source.util.TreePath; import com.sun.source.util.Trees; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; @@ -42,6 +43,8 @@ import java.util.Set; import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; @@ -72,6 +75,7 @@ import org.netbeans.api.java.source.TreeUtilities; import org.netbeans.api.java.source.support.ReferencesCount; import org.netbeans.api.lexer.Token; import org.netbeans.api.lexer.TokenSequence; +import org.netbeans.api.lexer.TokenUtilities; import org.netbeans.modules.java.completion.Utilities; import org.netbeans.modules.java.editor.base.javadoc.JavadocCompletionUtils; import org.netbeans.modules.java.editor.javadoc.TagRegistery.TagEntry; @@ -1212,6 +1216,12 @@ public class JavadocCompletionTask<T> extends UserTask { CharSequence text = token.text(); int pos = caretOffset - jdts.offset(); DocTreePath tag = getTag(jdctx, caretOffset); + int startPos = (int) jdctx.positions.getStartPosition(jdctx.javac.getCompilationUnit(), jdctx.comment, tag.getLeaf()); + String subStr = JavadocCompletionUtils.getCharSequence(jdctx.doc, startPos, caretOffset).toString(); + int index = subStr.lastIndexOf("\n"); + String markupLine = JavadocCompletionUtils.getCharSequence(jdctx.doc, (index + startPos), caretOffset).toString(); + insideInlineSnippet(markupLine); + if (pos > 0 && pos <= text.length() && text.charAt(pos - 1) == '{') { if (tag != null && !JavadocCompletionUtils.isBlockTag(tag)) { int start = (int) jdctx.positions.getStartPosition(jdctx.javac.getCompilationUnit(), jdctx.comment, tag.getLeaf()); @@ -1232,6 +1242,65 @@ public class JavadocCompletionTask<T> extends UserTask { } } + void insideInlineSnippet(String subStr) { + if (subStr.contains("//")) { + if (subStr.endsWith("@")) { + List<String> inlineAttr = new ArrayList() { + { + add("highlight"); + add("replace"); + add("link"); + add("start"); + add("end"); + } + }; + for (String str : inlineAttr) { + items.add(factory.createNameItem(str, this.caretOffset)); + } + } else { + String[] tags = {"@highlight", "@replace", "@link", "@start", "@end"}; + Matcher match = Pattern.compile("@\\b\\w{1,}\\b\\s+(?!.*@\\b\\w{1,}\\b\\s+)").matcher(subStr); + if (match.find() && Arrays.asList(tags).contains(match.group(0).trim())) { + completeInlineMarkupTag(match.group(0).trim(), new ArrayList() { + { + add("substring"); + add("regex"); + add("region"); + } + }); + } + } + } + } + + private void completeInlineMarkupTag(String str, List<String> attr) { + String value = " = \"<value>\""; + switch (str) { + case "@highlight": + attr.add("type"); + break; + case "@replace": + attr.add("replacement"); + break; + case "@link": + attr.add("target"); + attr.add("type"); + break; + case "@start": + case "@end": + attr.clear(); + attr.add("region"); + break; + default: + break; + } + if (!attr.isEmpty()) { + for (String entry : attr) { + items.add(factory.createNameItem(entry + value, this.caretOffset)); + } + } + } + private static class JavadocContext { private final CompilationInfo javac; diff --git a/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/javadoc/JavaDocCompletionTaskTest.java b/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/javadoc/JavaDocCompletionTaskTest.java new file mode 100644 index 0000000000..74c5f5b746 --- /dev/null +++ b/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/javadoc/JavaDocCompletionTaskTest.java @@ -0,0 +1,215 @@ +/* + * 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.java.editor.javadoc; + +import java.util.List; +import java.util.ArrayList; +import org.netbeans.modules.java.editor.base.javadoc.JavadocTestSupport; + +/** + * + * @author mjayan + */ +public class JavaDocCompletionTaskTest extends JavadocTestSupport { + + public JavaDocCompletionTaskTest(String name) { + super(name); + } + + public void testMarkupTagHint() throws Exception { + String code = "System.out.println(arg); //@"; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + for (JavadocCompletionItem item : list) { + listStr.add(item.getSortText().toString()); + } + List<String> inlineAttr = new ArrayList() { + { + add("highlight"); + add("replace"); + add("link"); + add("start"); + add("end"); + } + }; + assertEquals(listStr, inlineAttr); + } + + public void testMarkupTagHintNoMatch() throws Exception { + String code = "System.out.println(arg); //@qwerty "; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + assertEquals(list, listStr); + } + + public void testHighlightTagHint() throws Exception { + String code = "System.out.println(arg); //@highlight "; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + for (JavadocCompletionItem item : list) { + listStr.add(item.getSortText().toString()); + } + String value = " = \"<value>\""; + List<String> inlineAttr = new ArrayList() { + { + add("substring" + value); + add("regex" + value); + add("region" + value); + add("type" + value); + } + }; + assertEquals(listStr, inlineAttr); + } + + public void testReplaceTagHint() throws Exception { + String code = "System.out.println(arg); //@replace "; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + for (JavadocCompletionItem item : list) { + listStr.add(item.getSortText().toString()); + } + String value = " = \"<value>\""; + List<String> inlineAttr = new ArrayList() { + { + add("substring" + value); + add("regex" + value); + add("region" + value); + add("replacement" + value); + } + }; + assertEquals(listStr, inlineAttr); + } + + public void testLinkTagHint() throws Exception { + String code = "System.out.println(arg); //@link "; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + for (JavadocCompletionItem item : list) { + listStr.add(item.getSortText().toString()); + } + String value = " = \"<value>\""; + List<String> inlineAttr = new ArrayList() { + { + add("substring" + value); + add("regex" + value); + add("region" + value); + add("target" + value); + add("type" + value); + } + }; + assertEquals(listStr, inlineAttr); + } + + public void testStartTagHint() throws Exception { + String code = "System.out.println(arg); //@start "; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + for (JavadocCompletionItem item : list) { + listStr.add(item.getSortText().toString()); + } + String value = " = \"<value>\""; + List<String> inlineAttr = new ArrayList() { + { + add("region" + value); + } + }; + assertEquals(listStr, inlineAttr); + } + + public void testEndTagHint() throws Exception { + String code = "System.out.println(arg); //@end "; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + for (JavadocCompletionItem item : list) { + listStr.add(item.getSortText().toString()); + } + String value = " = \"<value>\""; + List<String> inlineAttr = new ArrayList() { + { + add("region" + value); + } + }; + assertEquals(listStr, inlineAttr); + } + + public void testMultipleTags() throws Exception { + String code = "System.out.println(arg); //@highlight region @"; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + for (JavadocCompletionItem item : list) { + listStr.add(item.getSortText().toString()); + } + List<String> inlineAttr = new ArrayList() { + { + add("highlight"); + add("replace"); + add("link"); + add("start"); + add("end"); + } + }; + assertEquals(listStr, inlineAttr); + } + + public void testMultipleAttr() throws Exception { + String code = "System.out.println(arg); //@highlight region=\"<value>\" "; + JavadocCompletionTask task = JavadocCompletionTask.create(0, new JavadocCompletionItem.Factory(), false, null); + task.insideInlineSnippet(code); + List<JavadocCompletionItem> list = task.getResults(); + assertNotNull(list); + List<String> listStr = new ArrayList<>(); + for (JavadocCompletionItem item : list) { + listStr.add(item.getSortText().toString()); + } + String value = " = \"<value>\""; + List<String> inlineAttr = new ArrayList() { + { + add("substring" + value); + add("regex" + value); + add("region" + value); + add("type" + value); + } + }; + assertEquals(listStr, inlineAttr); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists