This is an automated email from the ASF dual-hosted git repository. matthiasblaesing 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 3e58e3c60c Optimize matching for CSS completion queries new 0618061a28 Merge pull request #4979 from matthiasblaesing/css_documentation 3e58e3c60c is described below commit 3e58e3c60caa70f35929c6d2a98621cbc19623eb Author: Matthias Bläsing <mblaes...@doppel-helix.eu> AuthorDate: Wed Nov 16 21:56:58 2022 +0100 Optimize matching for CSS completion queries There are two different issues: - The documentation is fetched based on the completion result. This was only build based on the prefix. This change sorts the completion result using the node image where the cursor is currently located and sort by levenshtein distance between this image and the completion proposal. This result in preferring better matching entries. - The documentation is looked up from the W3C specificiation HTML files. To do this the correct location is located using several regular expressions. To make this more precise a less flexible regexp is used. The other matches are retained and used as fallbacks. Closes: #4778 --- .../modules/css/editor/csl/CssCompletion.java | 45 +++++++++++++++++++++- .../main/StandardPropertiesHelpResolver.java | 7 +++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/ide/css.editor/src/org/netbeans/modules/css/editor/csl/CssCompletion.java b/ide/css.editor/src/org/netbeans/modules/css/editor/csl/CssCompletion.java index 4fc4d40875..9f87b66459 100644 --- a/ide/css.editor/src/org/netbeans/modules/css/editor/csl/CssCompletion.java +++ b/ide/css.editor/src/org/netbeans/modules/css/editor/csl/CssCompletion.java @@ -38,6 +38,7 @@ import javax.swing.ImageIcon; import javax.swing.text.Caret; import javax.swing.text.Document; import javax.swing.text.JTextComponent; +import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.editor.EditorRegistry; import org.netbeans.api.lexer.Token; import org.netbeans.api.lexer.TokenHierarchy; @@ -50,7 +51,6 @@ import org.netbeans.modules.csl.api.CompletionProposal; import org.netbeans.modules.csl.api.ElementHandle; import org.netbeans.modules.csl.api.ElementHandle.UrlHandle; import org.netbeans.modules.csl.api.ElementKind; -import org.netbeans.modules.csl.api.OffsetRange; import org.netbeans.modules.csl.api.ParameterInfo; import org.netbeans.modules.csl.spi.DefaultCompletionResult; import org.netbeans.modules.csl.spi.ParserResult; @@ -109,7 +109,7 @@ public class CssCompletion implements CodeCompletionHandler { @Override public CodeCompletionResult complete(CodeCompletionContext context) { - final List<CompletionProposal> completionProposals = new ArrayList<>(); + List<CompletionProposal> completionProposals = new ArrayList<>(); CssParserResult info = (CssParserResult) context.getParserResult(); Snapshot snapshot = info.getSnapshot(); @@ -235,6 +235,13 @@ public class CssCompletion implements CodeCompletionHandler { completePropertyName(completionContext, completionProposals); completePropertyValue(completionContext, completionProposals, charAfterCaret); + String nodeImage = node.image().toString(); + completionProposals.sort((a, b) -> { + int distA = levenshteinDistance(nodeImage, a.getName(), false); + int distB = levenshteinDistance(nodeImage, b.getName(), false); + return distA - distB; + }); + return new DefaultCompletionResult(completionProposals, false); } @@ -1682,6 +1689,40 @@ public class CssCompletion implements CodeCompletionHandler { } //switch } + @SuppressWarnings("AssignmentToMethodParameter") + public final int levenshteinDistance( + @NonNull String str1, + @NonNull String str2, + final boolean caseSensitive) { + if (!caseSensitive) { + str1 = str1.toLowerCase(); + str2 = str2.toLowerCase(); + } + int[][] distance = new int[str1.length() + 1][str2.length() + 1]; + + for (int i = 0; i <= str1.length(); i++) { + distance[i][0] = i; + } + for (int j = 1; j <= str2.length(); j++) { + distance[0][j] = j; + } + + for (int i = 1; i <= str1.length(); i++) { + for (int j = 1; j <= str2.length(); j++) { + distance[i][j] = minimum( + distance[i - 1][j] + 1, + distance[i][j - 1] + 1, + distance[i - 1][j - 1] + ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0 : 1)); + } + } + + return distance[str1.length()][str2.length()]; + } + + private static int minimum(int a, int b, int c) { + return Math.min(Math.min(a, b), c); + } + private static class CssFileCompletionResult extends DefaultCompletionResult { private int moveCaretBack; diff --git a/ide/css.editor/src/org/netbeans/modules/css/editor/module/main/StandardPropertiesHelpResolver.java b/ide/css.editor/src/org/netbeans/modules/css/editor/module/main/StandardPropertiesHelpResolver.java index e29308feaf..b2cd28675c 100644 --- a/ide/css.editor/src/org/netbeans/modules/css/editor/module/main/StandardPropertiesHelpResolver.java +++ b/ide/css.editor/src/org/netbeans/modules/css/editor/module/main/StandardPropertiesHelpResolver.java @@ -105,10 +105,15 @@ public class StandardPropertiesHelpResolver extends HelpResolver { String elementName = "dfn"; - String patternImg = String.format("(?s)<%s[^>]*id=['\"]?\\w*-??propdef-%s\\d?['\"]?[^>]*>", elementName, propertyName); + String patternImg = String.format("(?s)<%s[^>]*id=(['\"])propdef-%s\\d?\\1[^>]*>", elementName, propertyName); Pattern pattern = Pattern.compile(patternImg); //DOTALL mode Matcher matcher = pattern.matcher(urlContent); + if (!matcher.find(0)){ + patternImg = String.format("(?s)<%s[^>]*id=['\"]?\\w*-??propdef-%s\\d?['\"]?[^>]*>", elementName, propertyName); + pattern = Pattern.compile(patternImg); //DOTALL mode + matcher = pattern.matcher(urlContent); + } if (!matcher.find(0)){ patternImg = String.format("(?s)<%s[^>]*id=['\"]?\\w*-??%s\\d?['\"]?>", elementName, propertyName); pattern = Pattern.compile(patternImg); //DOTALL mode --------------------------------------------------------------------- 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