Search in sources :

Example 6 with IRegion

use of org.springframework.ide.vscode.commons.util.text.IRegion 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 7 with IRegion

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

the class YamlHoverInfoProvider method getHoverInfo.

@Override
public Tuple2<Renderable, IRegion> getHoverInfo(IDocument doc, int offset) throws Exception {
    YamlFileAST ast = getAst(doc);
    if (ast != null) {
        IRegion region = getHoverRegion(ast, offset);
        if (region != null) {
            YamlDocument ymlDoc = new YamlDocument(doc, structureProvider);
            YamlAssistContext assistContext = assistContextProvider.getGlobalAssistContext(ymlDoc);
            if (assistContext != null) {
                List<NodeRef<?>> astPath = ast.findPath(offset);
                final YamlPath path = YamlPath.fromASTPath(astPath);
                if (path != null) {
                    YamlPath assistPath = path;
                    if (assistPath.pointsAtKey()) {
                        // When a path points at a key we must tramsform it to a
                        // 'value-terminating path'
                        // to be able to reuse the 'getHoverInfo' method on
                        // YamlAssistContext (as navigation
                        // into 'key' is not defined for YamlAssistContext.
                        String key = path.getLastSegment().toPropString();
                        assistPath = path.dropLast().append(YamlPathSegment.valueAt(key));
                    }
                    assistContext = assistPath.traverse(assistContext);
                    if (assistContext != null) {
                        Renderable info = path.pointsAtValue() ? assistContext.getValueHoverInfo(ymlDoc, new DocumentRegion(doc, region)) : assistContext.getHoverInfo();
                        // Fix for: PT 134914895. If assist context cannot provide an info, then don't return a Tuple.
                        if (info != null) {
                            return Tuples.of(info, region);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : NodeRef(org.springframework.ide.vscode.commons.yaml.ast.NodeRef) YamlPath(org.springframework.ide.vscode.commons.yaml.path.YamlPath) Renderable(org.springframework.ide.vscode.commons.util.Renderable) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST) YamlDocument(org.springframework.ide.vscode.commons.yaml.structure.YamlDocument) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) YamlAssistContext(org.springframework.ide.vscode.commons.yaml.completion.YamlAssistContext) IRegion(org.springframework.ide.vscode.commons.util.text.IRegion)

Example 8 with IRegion

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

the class YamlQuickfixes method getCursorPostionAfter.

private Position getCursorPostionAfter(TextDocument _doc, YamlPathEdits edits) {
    try {
        IRegion newSelection = edits.getSelection();
        if (newSelection != null) {
            // There is probably a more efficient way to compute the new cursor position. But its tricky...
            // ... because we need to compute line/char coordinate, in terms of lines in the *new* document.
            // So we have to take into account how newlines have been inserted or shifted around by the edits.
            // Doing that without actually applying the edits is... difficult.
            TextDocument doc = _doc.copy();
            edits.apply(doc);
            return doc.toPosition(newSelection.getOffset());
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return null;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) IRegion(org.springframework.ide.vscode.commons.util.text.IRegion)

Example 9 with IRegion

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

the class YamlDocument method isCommented.

/**
 * Determine whether given offset is inside a comment.
 */
public boolean isCommented(int offset) throws Exception {
    // Yaml only has end of line comments marked with a '#'.
    // So comments never span multiple lines of text and we only have scan back
    // from offset upto the start of the current line.
    IRegion lineInfo = doc.getLineInformationOfOffset(offset);
    if (lineInfo != null) {
        int startOfLine = lineInfo.getOffset();
        while (offset >= startOfLine) {
            char c = getChar(offset);
            if (c == '#') {
                return true;
            }
            offset--;
        }
    }
    return false;
}
Also used : IRegion(org.springframework.ide.vscode.commons.util.text.IRegion)

Example 10 with IRegion

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

the class YamlDocument method getLineIndentation.

/**
 * Returns the number of leading spaces in front of a line. If the line is effectively empty (only contains
 * comments and/or spaces then this returns -1 (meaning undefined, as indentation level only really means
 * something for lines which have 'real' content.
 */
public int getLineIndentation(int line) {
    IRegion r;
    try {
        r = getLineInformation(line);
    } catch (BadLocationException e) {
        // not a line in the document so it has no indentation
        return -1;
    }
    int len = r.getLength();
    int startOfLine = r.getOffset();
    int leadingSpaces = 0;
    while (leadingSpaces < len) {
        char c = getChar(startOfLine + leadingSpaces);
        if (c == ' ') {
            leadingSpaces++;
        } else if (c == '#') {
            return -1;
        } else if (c != ' ') {
            return leadingSpaces;
        }
        leadingSpaces++;
    }
    // Whole line scanned and nothing but spaces found
    return -1;
}
Also used : IRegion(org.springframework.ide.vscode.commons.util.text.IRegion) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Aggregations

IRegion (org.springframework.ide.vscode.commons.util.text.IRegion)10 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)4 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)3 Renderable (org.springframework.ide.vscode.commons.util.Renderable)3 Region (org.springframework.ide.vscode.commons.util.text.Region)2 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)2 ImmutableList (com.google.common.collect.ImmutableList)1 Builder (com.google.common.collect.ImmutableList.Builder)1 Collection (java.util.Collection)1 Optional (java.util.Optional)1 Hover (org.eclipse.lsp4j.Hover)1 Range (org.eclipse.lsp4j.Range)1 SPACES (org.springframework.ide.vscode.boot.common.CommonLanguageTools.SPACES)1 CommonLanguageTools.getValueHints (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueHints)1 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)1 InformationTemplates (org.springframework.ide.vscode.boot.common.InformationTemplates)1 PropertyInfo (org.springframework.ide.vscode.boot.metadata.PropertyInfo)1 SpringPropertyIndex (org.springframework.ide.vscode.boot.metadata.SpringPropertyIndex)1 StsValueHint (org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)1 Type (org.springframework.ide.vscode.boot.metadata.types.Type)1