Search in sources :

Example 36 with TextDocument

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

the class SimpleTextDocumentService method didOpen.

@Override
public void didOpen(DidOpenTextDocumentParams params) {
    async.execute(() -> {
        TextDocumentItem docId = params.getTextDocument();
        String url = docId.getUri();
        // Log.info("didOpen: "+params.getTextDocument().getUri());
        LanguageId languageId = LanguageId.of(docId.getLanguageId());
        int version = docId.getVersion();
        if (url != null) {
            String text = params.getTextDocument().getText();
            TrackedDocument td = createDocument(url, languageId, version, text).open();
            // Log.info("Opened "+td.getOpenCount()+" times: "+url);
            TextDocument doc = td.getDocument();
            TextDocumentContentChangeEvent change = new TextDocumentContentChangeEvent() {

                @Override
                public Range getRange() {
                    return null;
                }

                @Override
                public Integer getRangeLength() {
                    return null;
                }

                @Override
                public String getText() {
                    return text;
                }
            };
            TextDocumentContentChange evt = new TextDocumentContentChange(doc, ImmutableList.of(change));
            documentChangeListeners.fire(evt);
        }
    });
}
Also used : TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) LanguageId(org.springframework.ide.vscode.commons.util.text.LanguageId) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Example 37 with TextDocument

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

the class SimpleTextDocumentService method didChange.

@Override
public final void didChange(DidChangeTextDocumentParams params) {
    async.execute(() -> {
        try {
            VersionedTextDocumentIdentifier docId = params.getTextDocument();
            String url = docId.getUri();
            // Log.debug("didChange: "+url);
            if (url != null) {
                TextDocument doc = getDocument(url);
                List<TextDocumentContentChangeEvent> changes = params.getContentChanges();
                doc.apply(params);
                didChangeContent(doc, changes);
            }
        } catch (BadLocationException e) {
            Log.log(e);
        }
    });
}
Also used : VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 38 with TextDocument

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

the class SimpleTextDocumentService method createDocument.

private synchronized TrackedDocument createDocument(String url, LanguageId languageId, int version, String text) {
    TrackedDocument existingDoc = documents.get(url);
    if (existingDoc != null) {
        Log.warn("Creating document [" + url + "] but it already exists. Reusing existing!");
        return existingDoc;
    }
    TrackedDocument doc = new TrackedDocument(new TextDocument(url, languageId, version, text));
    documents.put(url, doc);
    return doc;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument)

Example 39 with TextDocument

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

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

the class GithubValueParsers method uri.

public static ValueParser uri(GithubInfoProvider github) {
    return new ValueParser() {

        @Override
        public GithubRepoReference parse(String _str) throws Exception {
            TextDocument doc = new TextDocument(null, LanguageId.PLAINTEXT);
            doc.setText(_str);
            DocumentRegion str = new DocumentRegion(doc);
            GithubRepoReference repo = parseFormat(str);
            if (repo != null) {
                Collection<String> knownRepos;
                try {
                    knownRepos = github.getReposForOwner(repo.owner.toString());
                } catch (Exception e) {
                    // Couldn't read info from github. Ignore this in reconciler context.
                    return repo;
                }
                if (knownRepos == null) {
                    throw unknownEntity("User or Organization not found: '" + repo.owner + "'", repo.owner);
                } else {
                    if (!knownRepos.contains(repo.name.toString())) {
                        throw unknownEntity("Repo not found: '" + repo.name + "'", repo.name);
                    }
                }
            }
            return repo;
        }

        private GithubRepoReference parseFormat(DocumentRegion str) throws Exception {
            String prefix = checkPrefix(str);
            if (prefix != null) {
                DocumentRegion ownerAndName = str.subSequence(prefix.length());
                // Should end with '.git'
                if (ownerAndName.endsWith(".git")) {
                    ownerAndName = ownerAndName.subSequence(0, ownerAndName.length() - 4);
                } else {
                    DocumentRegion highlight = ownerAndName.textAtEnd(1);
                    throw new ValueParseException("GitHub repo uri should end with '.git'", highlight.getStart(), highlight.getEnd());
                }
                int slash = ownerAndName.indexOf('/');
                if (slash >= 0) {
                    return new GithubRepoReference(ownerAndName.subSequence(0, slash), ownerAndName.subSequence(slash + 1));
                } else {
                    throw new ValueParseException("Expecting something of the form '${owner}/${repo}'.", ownerAndName.getStart(), ownerAndName.getEnd());
                }
            }
            return null;
        }

        private String checkPrefix(DocumentRegion str) throws ValueParseException {
            for (String expectedPrefix : GithubRepoContentAssistant.URI_PREFIXES) {
                int lastChar = expectedPrefix.length() - 1;
                if (str.startsWith(expectedPrefix.substring(0, lastChar))) {
                    if (str.charAt(lastChar) == expectedPrefix.charAt(lastChar)) {
                        return expectedPrefix;
                    }
                    throw new ValueParseException("Expecting a '" + expectedPrefix.charAt(lastChar) + "'", lastChar, lastChar + 1);
                }
            }
            return null;
        }
    };
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ReconcileException(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

Aggregations

TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)43 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)12 URI (java.net.URI)11 File (java.io.File)10 Location (org.eclipse.lsp4j.Location)8 Test (org.junit.Test)8 Path (java.nio.file.Path)7 ArrayList (java.util.ArrayList)7 SymbolInformation (org.eclipse.lsp4j.SymbolInformation)7 IClasspath (org.springframework.ide.vscode.commons.java.IClasspath)7 IJavaProject (org.springframework.ide.vscode.commons.java.IJavaProject)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 Range (org.eclipse.lsp4j.Range)6 Collection (java.util.Collection)5 Map (java.util.Map)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Stream (java.util.stream.Stream)5 ASTParser (org.eclipse.jdt.core.dom.ASTParser)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5