Search in sources :

Example 1 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class AbstractYamlAssistContext method getCustomAssistRegion.

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;
            // assumes we only look at the current line, good enough for now
            int valueEnd = keyNode.getNodeEnd();
            DocumentRegion region = new DocumentRegion(doc.getDocument(), valueStart, valueEnd);
            if (region.startsWith(" ")) {
                region = region.subSequence(1);
            }
            return region;
        }
    }
    // TODO Reaching here might mean support for calling the custom assistant isn't
    return null;
// implemented for this kind of context yet. It will have to be expanded upon
// as the need for it arises in real use-cases.
}
Also used : SKeyNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion)

Example 2 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class YTypeAssistContext method getCompletions.

@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> completions = getValueCompletions(doc, node, offset, query);
    if (completions.isEmpty()) {
        TypeBasedSnippetProvider snippetProvider = typeUtil.getSnippetProvider();
        if (snippetProvider != null) {
            Collection<Snippet> snippets = snippetProvider.getSnippets(type);
            YamlIndentUtil indenter = new YamlIndentUtil(doc);
            for (Snippet snippet : snippets) {
                String snippetName = snippet.getName();
                double score = FuzzyMatcher.matchScore(query, snippetName);
                if (score != 0.0 && snippet.isApplicable(getSchemaContext())) {
                    DocumentEdits edits = createEditFromSnippet(doc, node, offset, query, indenter, snippet);
                    completions.add(completionFactory().valueProposal(snippetName, query, snippetName, type, null, score, edits, typeUtil));
                }
            }
        }
        completions.addAll(getKeyCompletions(doc, node, offset, query));
    }
    if (typeUtil.isSequencable(type)) {
        completions = new ArrayList<>(completions);
        completions.addAll(getDashedCompletions(doc, node, offset));
    }
    return completions;
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) TypeBasedSnippetProvider(org.springframework.ide.vscode.commons.yaml.snippet.TypeBasedSnippetProvider) Snippet(org.springframework.ide.vscode.commons.yaml.snippet.Snippet) ISubCompletionEngine(org.springframework.ide.vscode.commons.yaml.schema.ISubCompletionEngine)

Example 3 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class SchemaBasedYamlASTReconciler method dashesAtStartOf.

private DocumentRegion dashesAtStartOf(YamlFileAST ast, Node node) {
    try {
        int start = node.getStartMark().getIndex();
        DocumentRegion textBefore = new DocumentRegion(ast.getDocument(), 0, start).trimEnd(Pattern.compile("\\s*"));
        DocumentRegion dashes = textBefore.subSequence(textBefore.getLength() - 3);
        if (dashes.toString().equals("---")) {
            return dashes;
        }
    } catch (Exception e) {
        Log.log(e);
    }
    // something unexpected... we couldn't find the '---'. So just mark the entire node.
    return allOf(ast, node);
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) Constraint(org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint) ReconcileException(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

Example 4 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class SchemaBasedYamlASTReconciler method getRegion.

protected DocumentRegion getRegion(Exception e, IDocument doc, Node node) {
    DocumentRegion region = new DocumentRegion(doc, node.getStartMark().getIndex(), node.getEndMark().getIndex());
    if (e instanceof ValueParseException) {
        ValueParseException parseException = (ValueParseException) e;
        int start = parseException.getStartIndex() >= 0 ? Math.min(node.getStartMark().getIndex() + parseException.getStartIndex(), node.getEndMark().getIndex()) : node.getStartMark().getIndex();
        int end = parseException.getEndIndex() >= 0 ? Math.min(node.getStartMark().getIndex() + parseException.getEndIndex(), node.getEndMark().getIndex()) : node.getEndMark().getIndex();
        region = new DocumentRegion(doc, start, end);
    }
    return region;
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) Constraint(org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint)

Example 5 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class PropertiesCompletionProposalsCalculator method getNavigationProposals.

private Collection<ICompletionProposal> getNavigationProposals() {
    String navPrefix = navigationPrefixFinder.getPrefix(doc, offset);
    try {
        if (navPrefix != null) {
            // offset of 'nav' operator char (i.e. '.' or ']').
            int navOffset = offset - navPrefix.length() - 1;
            navPrefix = fuzzySearchPrefix.getPrefix(doc, navOffset);
            if (navPrefix != null && !navPrefix.isEmpty()) {
                PropertyInfo prop = findLongestValidProperty(index, navPrefix);
                if (prop != null) {
                    int regionStart = navOffset - navPrefix.length();
                    Collection<ICompletionProposal> hintProposals = getKeyHintProposals(prop, navOffset);
                    if (CollectionUtil.hasElements(hintProposals)) {
                        return hintProposals;
                    }
                    PropertyNavigator navigator = new PropertyNavigator(doc, null, typeUtil, new DocumentRegion(doc, regionStart, navOffset));
                    Type type = navigator.navigate(regionStart + prop.getId().length(), TypeParser.parse(prop.getType()));
                    if (type != null) {
                        return getNavigationProposals(type, navOffset);
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return Collections.emptyList();
}
Also used : CommonLanguageTools.getValueType(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType) Type(org.springframework.ide.vscode.boot.metadata.types.Type) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) PropertyNavigator(org.springframework.ide.vscode.boot.properties.reconcile.PropertyNavigator) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Aggregations

DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)25 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)6 PropertyInfo (org.springframework.ide.vscode.boot.metadata.PropertyInfo)5 ArrayList (java.util.ArrayList)4 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)4 ValueParseException (org.springframework.ide.vscode.commons.util.ValueParseException)4 StsValueHint (org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)3 Type (org.springframework.ide.vscode.boot.metadata.types.Type)3 DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)3 Renderable (org.springframework.ide.vscode.commons.util.Renderable)3 IRegion (org.springframework.ide.vscode.commons.util.text.IRegion)3 ImmutableList (com.google.common.collect.ImmutableList)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 Location (org.eclipse.lsp4j.Location)2 SymbolInformation (org.eclipse.lsp4j.SymbolInformation)2 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)2 EnumCaseMode (org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode)2 PropertyNavigator (org.springframework.ide.vscode.boot.properties.reconcile.PropertyNavigator)2 ReconcileException (org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException)2 IDocument (org.springframework.ide.vscode.commons.util.text.IDocument)2