Content assist for 'domains' inside of route-strings

This commit is contained in:
Kris De Volder
2017-04-28 18:20:34 -07:00
parent 1966affe05
commit 5ccb3e5f22
13 changed files with 473 additions and 26 deletions

View File

@@ -11,6 +11,7 @@
package org.springframework.ide.vscode.commons.languageserver.completion;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -295,7 +296,7 @@ public class DocumentEdits implements ProposalApplier {
}
}
private ArrayList<Edit> edits = new ArrayList<Edit>();
private List<Edit> edits = new ArrayList<Edit>();
private IDocument doc;
public DocumentEdits(IDocument doc) {

View File

@@ -58,18 +58,17 @@ public class FuzzyMatcher {
//tend to favor matches at the end of the string over matches in the middle.
skips+=dlen-dpos; //but do count the extra chars at end => more extra = worse score
}
return score(gaps, skips);
return score(gaps, skips, pattern);
}
private static double score(int gaps, int skips) {
private static double score(int gaps, int skips, String pattern) {
if (gaps==0) {
//gaps == 0 means a prefix match, ignore 'skips' at end of String and just sort
// alphabetic (see STS-4049)
double badness = 0.1; // all scored equally, assumes using a 'stable' sorter.
return -badness; //higher is better
return 0.5+pattern.length(); //all scored equally, assumes using a 'stable' sorter.
} else {
double badness = 1+gaps + skips/10000.0; // higher is worse
return -badness; //higher is better
double badness = 1+gaps + skips/1000.0; // higher is worse
return 1.0/badness + pattern.length(); //higher is better
}
}

View File

@@ -56,6 +56,20 @@ public abstract class AbstractYamlAssistContext implements YamlAssistContext {
return doc;
}
protected DocumentRegion getCustomAssistRegion(YamlDocument doc, SNode node, int offset) {
if (node.getNodeType()==SNodeType.KEY) {
SKeyNode keyNode = (SKeyNode) node;
if (keyNode.isInValue(offset)) {
int valueStart = keyNode.getColonOffset()+1;
int valueEnd = keyNode.getNodeEnd(); // assumes we only look at the current line, good enough for now
return new DocumentRegion(doc.getDocument(), valueStart, valueEnd);
}
}
return null; // TODO Reaching here might mean support for calling the custom assistant isn't
// implemented for this kind of context yet. It will have to be expanded upon
// as the need for it arises in real use-cases.
}
protected final String getPrefix(YamlDocument doc, SNode node, int offset) {
//For value completions... in general we would like to determine the whole text
// corresponding to the value, so a simplistic backwards scan isn't good enough.

View File

@@ -84,7 +84,6 @@ public class DefaultCompletionFactory implements CompletionFactory {
public class ValueProposal extends ScoreableProposal {
private String value;
//private String query;
private String label;
private YType type;
private double baseScore;
@@ -94,7 +93,6 @@ public class DefaultCompletionFactory implements CompletionFactory {
public ValueProposal(String value, String query, String label, YType type, Renderable docs, double score, DocumentEdits edits, YTypeUtil typeUtil) {
this.value = value;
//this.query = query;
this.label = label;
this.type = type;
this.docs = docs;

View File

@@ -32,7 +32,9 @@ public abstract class TransformedCompletion extends ScoreableProposal {
this.original = proposal;
}
protected abstract String tranformLabel(String originalLabel);
protected String tranformLabel(String originalLabel) {
return originalLabel;
}
protected abstract DocumentEdits transformEdit(DocumentEdits textEdit);
@Override

View File

@@ -36,6 +36,7 @@ import org.springframework.ide.vscode.commons.yaml.path.YamlPath;
import org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment;
import org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment.YamlPathSegmentType;
import org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext;
import org.springframework.ide.vscode.commons.yaml.schema.ISubCompletionEngine;
import org.springframework.ide.vscode.commons.yaml.schema.SNodeDynamicSchemaContext;
import org.springframework.ide.vscode.commons.yaml.schema.YType;
import org.springframework.ide.vscode.commons.yaml.schema.YTypeUtil;
@@ -84,6 +85,13 @@ public class YTypeAssistContext extends AbstractYamlAssistContext {
@Override
public Collection<ICompletionProposal> getCompletions(YamlDocument doc, SNode node, int offset) throws Exception {
ISubCompletionEngine customContentAssistant = typeUtil.getCustomContentAssistant(type);
if (customContentAssistant!=null) {
DocumentRegion region = getCustomAssistRegion(doc, node, offset);
if (region!=null) {
return customContentAssistant.getCompletions(completionFactory(), region, region.toRelative(offset));
}
}
String query = getPrefix(doc, node, offset);
List<ICompletionProposal> valueCompletions = getValueCompletions(doc, node, offset, query);
if (!valueCompletions.isEmpty()) {

View File

@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.yaml.schema;
import java.util.List;
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionEngine;
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal;
import org.springframework.ide.vscode.commons.languageserver.util.DocumentRegion;
import org.springframework.ide.vscode.commons.yaml.completion.CompletionFactory;
/**
* Interface that can be used by a {@link ICompletionEngine} to delegate to a
* 'helper' completion engine that computes completions for some sub-region of
* the document.
*
* @author Kris De Volder
*/
@FunctionalInterface
public interface ISubCompletionEngine {
/**
* @param region The relevant subregion of the document (typically, the sub-engine only cares about the text
* within this particular region.)
* @param offset The cursor position relative to the region.
*/
List<ICompletionProposal> getCompletions(CompletionFactory f, DocumentRegion region, int offset);
}

View File

@@ -224,6 +224,11 @@ public class YTypeFactory {
public List<Constraint> getConstraints(YType type) {
return ((AbstractType)type).getConstraints();
}
@Override
public ISubCompletionEngine getCustomContentAssistant(YType type) {
return ((AbstractType)type).getCustomContentAssistant();
}
};
/////////////////////////////////////////////////////////////////////////////////////
@@ -239,11 +244,21 @@ public class YTypeFactory {
private Map<String, YTypedProperty> cachedPropertyMap;
private SchemaContextAware<Callable<Collection<YValueHint>>> hintProvider;
private List<Constraint> constraints = new ArrayList<>(2);
private ISubCompletionEngine customContentAssistant = null;
public boolean isSequenceable() {
return false;
}
public ISubCompletionEngine getCustomContentAssistant() {
return customContentAssistant;
}
public AbstractType setCustomContentAssistant(ISubCompletionEngine customContentAssistant) {
this.customContentAssistant = customContentAssistant;
return this;
}
public YType inferMoreSpecificType(DynamicSchemaContext dc) {
return this;
}

View File

@@ -13,6 +13,7 @@ package org.springframework.ide.vscode.commons.yaml.schema;
import java.util.List;
import java.util.Map;
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionEngine;
import org.springframework.ide.vscode.commons.util.ValueParser;
import org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint;
@@ -47,4 +48,6 @@ public interface YTypeUtil {
*/
YType inferMoreSpecificType(YType type, DynamicSchemaContext dc);
List<Constraint> getConstraints(YType type);
ISubCompletionEngine getCustomContentAssistant(YType type);
}