diff --git a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/yaml/completion/ScoreableProposal.java b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/yaml/completion/ScoreableProposal.java new file mode 100644 index 000000000..e9cf3d364 --- /dev/null +++ b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/yaml/completion/ScoreableProposal.java @@ -0,0 +1,107 @@ +package org.springframework.ide.vscode.commons.yaml.completion; + +import java.util.Comparator; + +import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal; + +public abstract class ScoreableProposal implements ICompletionProposal { + private static final double DEEMP_VALUE = 100000; // should be large enough to move deemphasized stuff to bottom of list. + + private double deemphasizedBy = 0.0; + + /** + * A sorter suitable for sorting ScoreableProposals based on their score. + */ + public static final Comparator COMPARATOR = new Comparator() { + public int compare(ICompletionProposal p1, ICompletionProposal p2) { + if (p1 instanceof ScoreableProposal && p2 instanceof ScoreableProposal) { + double s1 = ((ScoreableProposal)p1).getScore(); + double s2 = ((ScoreableProposal)p2).getScore(); + if (s1==s2) { + String name1 = ((ScoreableProposal)p1).getLabel(); + String name2 = ((ScoreableProposal)p2).getLabel(); + return name1.compareTo(name2); + } else { + return Double.compare(s2, s1); + } + } + return 0; + } + }; + public abstract double getBaseScore(); + public final double getScore() { + return getBaseScore() - deemphasizedBy; + } + public ScoreableProposal deemphasize() { + deemphasizedBy+= DEEMP_VALUE; + return this; + } + public boolean isDeemphasized() { + return deemphasizedBy > 0; + } + +// @Override +// public boolean isAutoInsertable() { +// return !isDeemphasized(); +// } + +// public StyledString getStyledDisplayString() { +// StyledString result = new StyledString(); +// highlightPattern(getHighlightPattern(), getBaseDisplayString(), result); +// return result; +// } + +// private void highlightPattern(String pattern, String data, StyledString result) { +// Styler highlightStyle = CompletionFactory.HIGHLIGHT; +// Styler plainStyle = isDeemphasized()?CompletionFactory.DEEMPHASIZE:CompletionFactory.NULL_STYLER; +// if (isDeprecated()) { +// highlightStyle = CompletionFactory.compose(highlightStyle, CompletionFactory.DEPRECATE); +// plainStyle = CompletionFactory.compose(plainStyle, CompletionFactory.DEPRECATE); +// } +// if (StringUtils.hasText(pattern)) { +// int dataPos = 0; int dataLen = data.length(); +// int patternPos = 0; int patternLen = pattern.length(); +// +// while (dataPos completions = new ArrayList<>(engine.getCompletions(doc, offset)); - Collections.sort(completions, DefaultCompletionFactory.COMPARATOR); + Collections.sort(completions, ScoreableProposal.COMPARATOR); CompletionListImpl list = new CompletionListImpl(); list.setIncomplete(false); List items = new ArrayList<>(completions.size()); @@ -105,7 +105,7 @@ public class VscodeCompletionEngineAdapter implements VscodeCompletionEngine { //Vscode applies some magic indent to a multi-line edit text. We do everything ourself so we have adjust for the magic // and do some kind of 'inverse magic' here. int vscodeMagicIndent = start.getCharacter(); - return YamlStructureParser.stripIndentation(vscodeMagicIndent, newText); + return StringUtil.stripIndentation(vscodeMagicIndent, newText); } @Override diff --git a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/StringUtil.java b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/StringUtil.java index 7583d37a1..ff14493f8 100644 --- a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/StringUtil.java +++ b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/StringUtil.java @@ -131,4 +131,48 @@ public class StringUtil { return f.format(d); } + private static final Pattern NEWLINE = Pattern.compile("(\\n|\\r)+"); + + /** + * Removes a given number of spaces from all lines of text in a String, + * except for the first line. + *

+ * Note: this method only deals with spaces its not suitable for strings + * which use tabs for indentation. + */ + public static String stripIndentation(int indent, String indentedText) { + StringBuilder out = new StringBuilder(); + boolean first = true; + Matcher matcher = NEWLINE.matcher(indentedText); + int pos = 0; + while (matcher.find()) { + int newline = matcher.start(); + int newline_end = matcher.end(); + String line = indentedText.substring(pos, newline); + if (first) { + first = false; + } else { + line = stripIndentationFromLine(indent, line); + } + out.append(line); + out.append(indentedText.substring(newline, newline_end)); + pos = newline_end; + } + String line = indentedText.substring(pos); + if (!first) { + line = stripIndentationFromLine(indent, line); + } + out.append(line); + return out.toString(); + } + + public static String stripIndentationFromLine(int indent, String line) { + int start = 0; + while (start COMPARATOR = new Comparator() { - public int compare(ICompletionProposal p1, ICompletionProposal p2) { - if (p1 instanceof ScoreableProposal && p2 instanceof ScoreableProposal) { - double s1 = ((ScoreableProposal)p1).getScore(); - double s2 = ((ScoreableProposal)p2).getScore(); - if (s1==s2) { - String name1 = ((ScoreableProposal)p1).getLabel(); - String name2 = ((ScoreableProposal)p2).getLabel(); - return name1.compareTo(name2); - } else { - return Double.compare(s2, s1); - } - } - return 0; - } - }; - - - public static abstract class ScoreableProposal implements ICompletionProposal { - private static final double DEEMP_VALUE = 100000; // should be large enough to move deemphasized stuff to bottom of list. - - private double deemphasizedBy = 0.0; - public abstract double getBaseScore(); - public final double getScore() { - return getBaseScore() - deemphasizedBy; - } - public ScoreableProposal deemphasize() { - deemphasizedBy+= DEEMP_VALUE; - return this; - } - public boolean isDeemphasized() { - return deemphasizedBy > 0; - } - -// @Override -// public boolean isAutoInsertable() { -// return !isDeemphasized(); -// } - -// public StyledString getStyledDisplayString() { -// StyledString result = new StyledString(); -// highlightPattern(getHighlightPattern(), getBaseDisplayString(), result); -// return result; -// } - -// private void highlightPattern(String pattern, String data, StyledString result) { -// Styler highlightStyle = CompletionFactory.HIGHLIGHT; -// Styler plainStyle = isDeemphasized()?CompletionFactory.DEEMPHASIZE:CompletionFactory.NULL_STYLER; -// if (isDeprecated()) { -// highlightStyle = CompletionFactory.compose(highlightStyle, CompletionFactory.DEPRECATE); -// plainStyle = CompletionFactory.compose(plainStyle, CompletionFactory.DEPRECATE); -// } -// if (StringUtils.hasText(pattern)) { -// int dataPos = 0; int dataLen = data.length(); -// int patternPos = 0; int patternLen = pattern.length(); -// -// while (dataPos0) { - return stripIndentation(indent, indentedText); + return StringUtil.stripIndentation(indent, indentedText); } return indentedText; } @@ -702,39 +701,4 @@ public class YamlStructureParser { return keyAliases.getKeyAliases(key); } - public static String stripIndentation(int indent, String indentedText) { - StringBuilder out = new StringBuilder(); - Pattern NEWLINE = Pattern.compile("(\\n|\\r)+"); - boolean first = true; - Matcher matcher = NEWLINE.matcher(indentedText); - int pos = 0; - while (matcher.find()) { - int newline = matcher.start(); - int newline_end = matcher.end(); - String line = indentedText.substring(pos, newline); - if (first) { - first = false; - } else { - line = stripIndentationFromLine(indent, line); - } - out.append(line); - out.append(indentedText.substring(newline, newline_end)); - pos = newline_end; - } - String line = indentedText.substring(pos); - if (!first) { - line = stripIndentationFromLine(indent, line); - } - out.append(line); - return out.toString(); - } - - private static String stripIndentationFromLine(int indent, String line) { - int start = 0; - while (start", "server:\n"+