Search in sources :

Example 16 with DocumentEdits

use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.

the class YamlCompletionEngine method dedented.

public ScoreableProposal dedented(ICompletionProposal proposal, int numSpacesToRemove, IDocument doc) {
    Assert.isLegal(numSpacesToRemove > 0);
    int spacesEnd = proposal.getTextEdit().getFirstEditStart();
    int spacesStart = spacesEnd - numSpacesToRemove;
    int numArrows = numSpacesToRemove / YamlIndentUtil.INDENT_BY;
    String spaces = new DocumentRegion(doc, spacesStart, spacesEnd).toString();
    YamlIndentUtil indenter = new YamlIndentUtil(doc);
    if (spaces.length() == numSpacesToRemove && SPACES.matcher(spaces).matches()) {
        ScoreableProposal transformed = new TransformedCompletion(proposal) {

            @Override
            public String tranformLabel(String originalLabel) {
                return Strings.repeat(Unicodes.LEFT_ARROW + " ", numArrows) + originalLabel;
            }

            @Override
            public DocumentEdits transformEdit(DocumentEdits originalEdit) {
                originalEdit.firstDelete(spacesStart, spacesEnd);
                originalEdit.transformFirstNonWhitespaceEdit((offset, insertText) -> {
                    String prefix = insertText.substring(0, offset);
                    String dedented = indenter.applyIndentation(insertText.substring(offset), -numSpacesToRemove);
                    return prefix + dedented;
                });
                return originalEdit;
            }

            @Override
            public String getFilterText() {
                // to replace. Since we are replacing these removed spaces, they must be part of the filtertext
                return spaces + super.getFilterText();
            }
        };
        transformed.deemphasize(DEEMP_DEDENTED_PROPOSAL * numArrows);
        return transformed;
    }
    // in our attempt to de-dent.)
    return null;
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) ScoreableProposal(org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion)

Example 17 with DocumentEdits

use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.

the class LanguageServerHarness method perform.

private void perform(WorkspaceEdit workspaceEdit) throws Exception {
    Assert.isNull("Versioned WorkspaceEdits not supported", workspaceEdit.getDocumentChanges());
    for (Entry<String, List<TextEdit>> entry : workspaceEdit.getChanges().entrySet()) {
        String uri = entry.getKey();
        TextDocumentInfo document = documents.get(uri);
        assertNotNull("Can't apply edits to non-existing document: " + uri, document);
        TextDocument workingDocument = new TextDocument(uri, document.getLanguageId());
        workingDocument.setText(document.getText());
        DocumentEdits edits = new DocumentEdits(workingDocument);
        for (TextEdit edit : entry.getValue()) {
            Range range = edit.getRange();
            edits.replace(document.toOffset(range.getStart()), document.toOffset(range.getEnd()), edit.getNewText());
        }
        edits.apply(workingDocument);
        Editor editor = getOpenEditor(uri);
        if (editor != null) {
            editor.setRawText(workingDocument.get());
        } else {
            changeDocument(uri, workingDocument.get());
        }
    }
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) TextEdit(org.eclipse.lsp4j.TextEdit) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CompletionList(org.eclipse.lsp4j.CompletionList) Range(org.eclipse.lsp4j.Range)

Example 18 with DocumentEdits

use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.

the class RouteContentAssistant method createProposal.

private ICompletionProposal createProposal(CompletionFactory f, DocumentRegion region, int offset, String query, double score, YValueHint domain) {
    DocumentEdits edits = new DocumentEdits(region.getDocument());
    region = region.subSequence(offset - query.length());
    // Add extra space after ':' if needed!
    boolean needSpace = region.textBefore(1).charAt(0) == ':';
    edits.replace(region.getStart(), region.getEnd(), needSpace ? " " + domain.getValue() : domain.getValue());
    return f.valueProposal(domain.getValue(), query, domain.getLabel(), schema.t_route_string, domain.getDocumentation(), score, edits, schema.getTypeUtil());
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)

Aggregations

DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)18 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)8 ArrayList (java.util.ArrayList)7 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)5 YamlIndentUtil (org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil)5 PropertyInfo (org.springframework.ide.vscode.boot.metadata.PropertyInfo)4 Match (org.springframework.ide.vscode.commons.util.FuzzyMap.Match)4 ScoreableProposal (org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal)3 YValueHint (org.springframework.ide.vscode.commons.yaml.schema.YValueHint)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)2 Type (org.springframework.ide.vscode.boot.metadata.types.Type)2 Renderable (org.springframework.ide.vscode.commons.util.Renderable)2 Strings (com.google.common.base.Strings)1 Supplier (com.google.common.base.Supplier)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1