diff options
author | 2021-10-31 01:53:11 +0200 | |
---|---|---|
committer | 2021-10-31 19:26:15 +0100 | |
commit | a7ba55f2b781544cd74927137025f2c00a398f69 (patch) | |
tree | 3fdb3a0bea8f2b53e238180b33388004368f6bd6 /language-ide/src/main/java/tools | |
parent | feat(web): find occurrences when idle (diff) | |
download | refinery-a7ba55f2b781544cd74927137025f2c00a398f69.tar.gz refinery-a7ba55f2b781544cd74927137025f2c00a398f69.tar.zst refinery-a7ba55f2b781544cd74927137025f2c00a398f69.zip |
fix(web): fix server-side content assist filtering
Diffstat (limited to 'language-ide/src/main/java/tools')
-rw-r--r-- | language-ide/src/main/java/tools/refinery/language/ide/ProblemIdeModule.java | 8 | ||||
-rw-r--r-- | language-ide/src/main/java/tools/refinery/language/ide/contentassist/FuzzyMatcher.java | 44 |
2 files changed, 51 insertions, 1 deletions
diff --git a/language-ide/src/main/java/tools/refinery/language/ide/ProblemIdeModule.java b/language-ide/src/main/java/tools/refinery/language/ide/ProblemIdeModule.java index 5327f7b4..3502c29f 100644 --- a/language-ide/src/main/java/tools/refinery/language/ide/ProblemIdeModule.java +++ b/language-ide/src/main/java/tools/refinery/language/ide/ProblemIdeModule.java | |||
@@ -3,16 +3,22 @@ | |||
3 | */ | 3 | */ |
4 | package tools.refinery.language.ide; | 4 | package tools.refinery.language.ide; |
5 | 5 | ||
6 | import org.eclipse.xtext.ide.editor.contentassist.IPrefixMatcher; | ||
6 | import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator; | 7 | import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator; |
7 | 8 | ||
9 | import tools.refinery.language.ide.contentassist.FuzzyMatcher; | ||
8 | import tools.refinery.language.ide.syntaxcoloring.ProblemSemanticHighlightingCalculator; | 10 | import tools.refinery.language.ide.syntaxcoloring.ProblemSemanticHighlightingCalculator; |
9 | 11 | ||
10 | /** | 12 | /** |
11 | * Use this class to register ide components. | 13 | * Use this class to register ide components. |
12 | */ | 14 | */ |
13 | public class ProblemIdeModule extends AbstractProblemIdeModule { | 15 | public class ProblemIdeModule extends AbstractProblemIdeModule { |
14 | |||
15 | public Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() { | 16 | public Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() { |
16 | return ProblemSemanticHighlightingCalculator.class; | 17 | return ProblemSemanticHighlightingCalculator.class; |
17 | } | 18 | } |
19 | |||
20 | @Override | ||
21 | public Class<? extends IPrefixMatcher> bindIPrefixMatcher() { | ||
22 | return FuzzyMatcher.class; | ||
23 | } | ||
18 | } | 24 | } |
diff --git a/language-ide/src/main/java/tools/refinery/language/ide/contentassist/FuzzyMatcher.java b/language-ide/src/main/java/tools/refinery/language/ide/contentassist/FuzzyMatcher.java new file mode 100644 index 00000000..fe722ca1 --- /dev/null +++ b/language-ide/src/main/java/tools/refinery/language/ide/contentassist/FuzzyMatcher.java | |||
@@ -0,0 +1,44 @@ | |||
1 | package tools.refinery.language.ide.contentassist; | ||
2 | |||
3 | import org.eclipse.xtext.ide.editor.contentassist.IPrefixMatcher; | ||
4 | |||
5 | import com.google.inject.Singleton; | ||
6 | |||
7 | /** | ||
8 | * Implements the candidate matching algoritm used by CodeMirror 6. | ||
9 | * | ||
10 | * Using this class ensures that the same candidates will be returned when | ||
11 | * filtering content assist proposals on the server as on the client. | ||
12 | * | ||
13 | * The matching is "fuzzy" (<code>fzf</code>-like), i.e., the prefix characters | ||
14 | * may occur anywhere in the name, but must be in the same order as in the | ||
15 | * prefix. | ||
16 | * | ||
17 | * @author Kristóf Marussy | ||
18 | */ | ||
19 | @Singleton | ||
20 | public class FuzzyMatcher implements IPrefixMatcher { | ||
21 | @Override | ||
22 | public boolean isCandidateMatchingPrefix(String name, String prefix) { | ||
23 | var nameIgnoreCase = name.toLowerCase(); | ||
24 | var prefixIgnoreCase = prefix.toLowerCase(); | ||
25 | int prefixLength = prefixIgnoreCase.length(); | ||
26 | if (prefixLength == 0) { | ||
27 | return true; | ||
28 | } | ||
29 | int nameLength = nameIgnoreCase.length(); | ||
30 | if (prefixLength > nameLength) { | ||
31 | return false; | ||
32 | } | ||
33 | int prefixIndex = 0; | ||
34 | for (int nameIndex = 0; nameIndex < nameLength; nameIndex++) { | ||
35 | if (nameIgnoreCase.charAt(nameIndex) == prefixIgnoreCase.charAt(prefixIndex)) { | ||
36 | prefixIndex++; | ||
37 | if (prefixIndex == prefixLength) { | ||
38 | return true; | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | return false; | ||
43 | } | ||
44 | } | ||