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);
}
});
}
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);
}
});
}
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;
}
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;
}
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;
}
};
}
Aggregations