use of org.graalvm.tools.lsp.server.types.Range in project graal by oracle.
the class SourceUtils method getRangeFrom.
public static Range getRangeFrom(Exception te, InteropLibrary interopLib) {
// TODO: LSP4J removal
Range range = Range.create(0, 0, 0, 0);
SourceSection sourceLocation;
try {
sourceLocation = interopLib.hasSourceLocation(te) ? interopLib.getSourceLocation(te) : null;
} catch (UnsupportedMessageException um) {
throw CompilerDirectives.shouldNotReachHere(um);
}
if (sourceLocation != null && sourceLocation.isAvailable()) {
range = sourceSectionToRange(sourceLocation);
}
return range;
}
use of org.graalvm.tools.lsp.server.types.Range in project graal by oracle.
the class SourceUtils method updateCoverageData.
private static void updateCoverageData(TextDocumentSurrogate surrogate, Source source, String newText, Range range, int replaceBegin, int replaceEnd, TruffleLogger logger) {
Source newSourceSnippet = Source.newBuilder("dummyLanguage", newText, "dummyCoverage").cached(false).build();
int linesNewText = newSourceSnippet.getLineCount() + (newText.endsWith("\n") ? 1 : 0) + (newText.isEmpty() ? 1 : 0);
Source oldSourceSnippet = source.subSource(replaceBegin, replaceEnd - replaceBegin);
int liensOldText = oldSourceSnippet.getLineCount() + (oldSourceSnippet.getCharacters().toString().endsWith("\n") ? 1 : 0) + (oldSourceSnippet.getLength() == 0 ? 1 : 0);
int newLineModification = linesNewText - liensOldText;
logger.log(Level.FINEST, "newLineModification: {0}", newLineModification);
if (newLineModification != 0) {
List<SourceSectionReference> sections = surrogate.getCoverageLocations();
sections.stream().filter(section -> section.includes(range)).forEach(section -> {
SourceSectionReference migratedSection = new SourceSectionReference(section);
migratedSection.setEndLine(migratedSection.getEndLine() + newLineModification);
surrogate.replace(section, migratedSection);
logger.log(Level.FINEST, "Included - Old: {0} Fixed: {1}", new Object[] { section, migratedSection });
});
sections.stream().filter(section -> section.behind(range)).forEach(section -> {
SourceSectionReference migratedSection = new SourceSectionReference(section);
migratedSection.setStartLine(migratedSection.getStartLine() + newLineModification);
migratedSection.setEndLine(migratedSection.getEndLine() + newLineModification);
surrogate.replace(section, migratedSection);
logger.log(Level.FINEST, "Behind - Old: {0} Fixed: {1}", new Object[] { section, migratedSection });
});
}
}
use of org.graalvm.tools.lsp.server.types.Range in project graal by oracle.
the class SourceUtils method applyTextDocumentChanges.
public static String applyTextDocumentChanges(List<? extends TextDocumentContentChangeEvent> list, Source source, TextDocumentSurrogate surrogate, TruffleLogger logger) {
Source currentSource = null;
String text = source.getCharacters().toString();
StringBuilder sb = new StringBuilder(text);
for (TextDocumentContentChangeEvent event : list) {
if (currentSource == null) {
currentSource = source;
} else {
currentSource = Source.newBuilder(currentSource.getLanguage(), sb, currentSource.getName()).cached(false).build();
}
Range range = event.getRange();
if (range == null) {
// The whole file has changed
// Clear StringBuilder
sb.setLength(0);
sb.append(event.getText());
continue;
}
Position start = range.getStart();
Position end = range.getEnd();
int startLine = start.getLine() + 1;
int endLine = end.getLine() + 1;
int replaceBegin = currentSource.getLineStartOffset(startLine) + start.getCharacter();
int replaceEnd = currentSource.getLineStartOffset(endLine) + end.getCharacter();
sb.replace(replaceBegin, replaceEnd, event.getText());
if (surrogate != null && surrogate.hasCoverageData()) {
updateCoverageData(surrogate, currentSource, event.getText(), range, replaceBegin, replaceEnd, logger);
}
}
return sb.toString();
}
use of org.graalvm.tools.lsp.server.types.Range in project graal by oracle.
the class SourceUtils method removeLastTextInsertion.
public static SourceFix removeLastTextInsertion(TextDocumentSurrogate surrogate, int originalCharacter, TruffleLogger logger) {
TextDocumentContentChangeEvent lastChange = surrogate.getLastChange();
if (lastChange == null) {
return null;
}
Range range = lastChange.getRange();
TextDocumentContentChangeEvent replacementEvent = //
TextDocumentContentChangeEvent.create("").setRange(//
Range.create(range.getStart(), Position.create(range.getEnd().getLine(), range.getEnd().getCharacter() + lastChange.getText().length()))).setRangeLength(lastChange.getText().length());
String codeBeforeLastChange = applyTextDocumentChanges(Arrays.asList(replacementEvent), surrogate.getSource(), surrogate, logger);
int characterIdx = originalCharacter - (originalCharacter - range.getStart().getCharacter());
return new SourceFix(codeBeforeLastChange, lastChange.getText(), characterIdx);
}
use of org.graalvm.tools.lsp.server.types.Range in project graal by oracle.
the class CoverageRequestHandler method getCoverageWithEnteredContext.
public Coverage getCoverageWithEnteredContext(URI uri) {
final TextDocumentSurrogate surrogate = surrogateMap.get(uri);
if (surrogate != null && surrogate.getSourceWrapper() != null && surrogate.getSourceWrapper().isParsingSuccessful()) {
final SourceSectionFilter filter = //
SourceSectionFilter.newBuilder().sourceIs(//
surrogate.getSourceWrapper().getSource()).tagIs(//
StatementTag.class).build();
final Set<SourceSection> duplicateFilter = new HashSet<>();
final List<Range> covered = new ArrayList<>();
final List<Range> uncovered = new ArrayList<>();
env.getInstrumenter().attachLoadSourceSectionListener(filter, new LoadSourceSectionListener() {
@Override
public void onLoad(LoadSourceSectionEvent event) {
SourceSection section = event.getSourceSection();
if (duplicateFilter.add(section)) {
if (surrogate.isLocationCovered(SourceSectionReference.from(section))) {
covered.add(SourceUtils.sourceSectionToRange(section));
} else {
uncovered.add(SourceUtils.sourceSectionToRange(section));
}
}
}
}, true).dispose();
return Coverage.create(covered, uncovered);
}
return null;
}
Aggregations