Search in sources :

Example 11 with DocumentRegion

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

the class GithubRepoContentAssistant method getCompletions.

@Override
public List<ICompletionProposal> getCompletions(CompletionFactory f, DocumentRegion region, int offset) {
    DocumentRegion query = region.subSequence(0, offset);
    // If uri prefix is already there, we provide CA for owner / repo
    for (String uriPrefix : URI_PREFIXES) {
        if (query.startsWith(uriPrefix)) {
            return getOwnerOrRepoCompletions(f, query.subSequence(uriPrefix.length()));
        }
    }
    // If uri prefix is not yet there, maybe we can suggest it (if it matches the query)
    List<ICompletionProposal> proposals = new ArrayList<>(URI_PREFIXES.length);
    for (String uriPrefix : URI_PREFIXES) {
        if (FuzzyMatcher.matchScore(query, uriPrefix) != 0.0) {
            proposals.add(SimpleCompletionFactory.simpleProposal(query, CompletionItemKind.Text, uriPrefix, null, null));
        }
    }
    return proposals;
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList)

Example 12 with DocumentRegion

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

the class PropertiesHoverCalculator method getValueHover.

private Tuple2<Renderable, IRegion> getValueHover(Value value) {
    DocumentRegion valueRegion = createRegion(doc, value).trimStart(SPACES).trimEnd(SPACES);
    if (valueRegion.getStart() <= offset && offset < valueRegion.getEnd()) {
        String valueString = valueRegion.toString();
        String propertyName = value.getParent().getKey().decode();
        Type type = getValueType(index, typeUtil, propertyName);
        if (TypeUtil.isSequencable(type)) {
            // It is useful to provide content assist for the values in the list when entering a list
            type = TypeUtil.getDomainType(type);
        }
        if (TypeUtil.isClass(type)) {
            // Special case. We want to provide hoverinfos more liberally than what's suggested for completions (i.e. even class names
            // that are not suggested by the hints because they do not meet subtyping constraints should be hoverable and linkable!
            StsValueHint hint = StsValueHint.className(valueString, typeUtil);
            if (hint != null) {
                return Tuples.of(createRenderable(hint), valueRegion.asRegion());
            }
        }
        // Hack: pretend to invoke content-assist at the end of the value text. This should provide hints applicable to that value
        // then show hoverinfo based on that. That way we can avoid duplication a lot of similar logic to compute hoverinfos and hyperlinks.
        Collection<StsValueHint> hints = getValueHints(index, typeUtil, valueString, propertyName, EnumCaseMode.ALIASED);
        if (hints != null) {
            Optional<StsValueHint> hint = hints.stream().filter(h -> valueString.equals(h.getValue())).findFirst();
            if (hint.isPresent()) {
                return Tuples.of(createRenderable(hint.get()), valueRegion.asRegion());
            }
        }
    }
    return null;
}
Also used : CommonLanguageTools.getValueType(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType) IDocument(org.springframework.ide.vscode.commons.util.text.IDocument) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) SpringPropertyIndex(org.springframework.ide.vscode.boot.metadata.SpringPropertyIndex) Tuples(reactor.util.function.Tuples) Type(org.springframework.ide.vscode.boot.metadata.types.Type) Tuple2(reactor.util.function.Tuple2) AntlrParser(org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser) ImmutableList(com.google.common.collect.ImmutableList) FuzzyMap(org.springframework.ide.vscode.commons.util.FuzzyMap) Renderables.paragraph(org.springframework.ide.vscode.commons.util.Renderables.paragraph) IRegion(org.springframework.ide.vscode.commons.util.text.IRegion) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) EnumCaseMode(org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode) Collection(java.util.Collection) SPACES(org.springframework.ide.vscode.boot.common.CommonLanguageTools.SPACES) Renderables.concat(org.springframework.ide.vscode.commons.util.Renderables.concat) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Renderables.bold(org.springframework.ide.vscode.commons.util.Renderables.bold) Renderables.text(org.springframework.ide.vscode.commons.util.Renderables.text) Key(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Key) InformationTemplates(org.springframework.ide.vscode.boot.common.InformationTemplates) Node(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Node) CommonLanguageTools.getValueHints(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueHints) Value(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Value) Optional(java.util.Optional) Renderable(org.springframework.ide.vscode.commons.util.Renderable) TypeUtil(org.springframework.ide.vscode.boot.metadata.types.TypeUtil) Builder(com.google.common.collect.ImmutableList.Builder) CommonLanguageTools.getValueType(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType) Type(org.springframework.ide.vscode.boot.metadata.types.Type) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion)

Example 13 with DocumentRegion

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

the class SpringPropertiesReconcileEngine method reconcile.

@Override
public void reconcile(IDocument doc, IProblemCollector problemCollector) {
    FuzzyMap<PropertyInfo> index = fIndexProvider.getIndex(doc);
    problemCollector.beginCollecting();
    try {
        ParseResults results = parser.parse(doc.get());
        DuplicateNameChecker duplicateNameChecker = new DuplicateNameChecker(problemCollector);
        results.syntaxErrors.forEach(syntaxError -> {
            problemCollector.accept(problem(PROP_SYNTAX_ERROR, syntaxError.getMessage(), syntaxError.getOffset(), syntaxError.getLength()));
        });
        if (index == null || index.isEmpty()) {
            // some problem putting information about properties into the index.
            return;
        }
        results.ast.getNodes(KeyValuePair.class).forEach(pair -> {
            try {
                DocumentRegion propertyNameRegion = createRegion(doc, pair.getKey());
                String keyName = PropertiesFileEscapes.unescape(propertyNameRegion.toString());
                duplicateNameChecker.check(propertyNameRegion);
                PropertyInfo validProperty = SpringPropertyIndex.findLongestValidProperty(index, keyName);
                if (validProperty != null) {
                    // in PropertyNavigator (probably these changes are also for the better making it simpler as well)
                    if (validProperty.isDeprecated()) {
                        problemCollector.accept(problemDeprecated(propertyNameRegion, validProperty));
                    }
                    int offset = validProperty.getId().length() + propertyNameRegion.getStart();
                    PropertyNavigator navigator = new PropertyNavigator(doc, problemCollector, typeUtilProvider.getTypeUtil(doc), propertyNameRegion);
                    Type valueType = navigator.navigate(offset, TypeParser.parse(validProperty.getType()));
                    if (valueType != null) {
                        reconcileType(doc, valueType, pair.getValue(), problemCollector);
                    }
                } else {
                    // validProperty==null
                    // The name is invalid, with no 'prefix' of the name being a valid property name.
                    PropertyInfo similarEntry = index.findLongestCommonPrefixEntry(propertyNameRegion.toString());
                    CharSequence validPrefix = commonPrefix(similarEntry.getId(), keyName);
                    problemCollector.accept(problemUnkownProperty(propertyNameRegion, similarEntry, validPrefix));
                }
            // end: validProperty==null
            } catch (Exception e) {
                Log.log(e);
            }
        });
    } catch (Throwable e2) {
        Log.log(e2);
    } finally {
        problemCollector.endCollecting();
    }
}
Also used : KeyValuePair(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Type(org.springframework.ide.vscode.boot.metadata.types.Type) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo)

Example 14 with DocumentRegion

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

the class ApplicationYamlASTReconciler method valueParseError.

private void valueParseError(YamlFileAST root, ScalarNode scalar, ValueParseException e) {
    IDocument doc = root.getDocument();
    DocumentRegion containingRegion = new DocumentRegion(doc, scalar.getStartMark().getIndex(), scalar.getEndMark().getIndex());
    problems.accept(problem(ApplicationYamlProblemType.YAML_VALUE_TYPE_MISMATCH, e.getHighlightRegion(containingRegion), ExceptionUtil.getMessage(e)));
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) IDocument(org.springframework.ide.vscode.commons.util.text.IDocument)

Example 15 with DocumentRegion

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

the class JavaSnippetManager method getCompletions.

public Collection<ICompletionProposal> getCompletions(IDocument doc, int offset, ASTNode node, CompilationUnit cu) {
    Collection<ICompletionProposal> completions = new ArrayList<>();
    DocumentRegion query = PREFIX_FINDER.getPrefixRegion(doc, offset);
    for (JavaSnippet javaSnippet : snippets) {
        if (FuzzyMatcher.matchScore(query.toString(), javaSnippet.getName()) != 0) {
            javaSnippet.generateCompletion(snippetBuilderFactory, query, node, cu).ifPresent((completion) -> completions.add(completion));
        }
    }
    return completions;
}
Also used : ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ArrayList(java.util.ArrayList)

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