Search in sources :

Example 1 with Range

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;
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) SourceSection(com.oracle.truffle.api.source.SourceSection) Range(org.graalvm.tools.lsp.server.types.Range)

Example 2 with 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 });
        });
    }
}
Also used : Arrays(java.util.Arrays) Position(org.graalvm.tools.lsp.server.types.Position) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent) TruffleLogger(com.oracle.truffle.api.TruffleLogger) Level(java.util.logging.Level) Range(org.graalvm.tools.lsp.server.types.Range) OptionValues(org.graalvm.options.OptionValues) List(java.util.List) Source(com.oracle.truffle.api.source.Source) CompilerDirectives(com.oracle.truffle.api.CompilerDirectives) Paths(java.nio.file.Paths) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) SourcePredicate(com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate) SourceSection(com.oracle.truffle.api.source.SourceSection) URI(java.net.URI) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) Path(java.nio.file.Path) Source(com.oracle.truffle.api.source.Source)

Example 3 with Range

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();
}
Also used : Position(org.graalvm.tools.lsp.server.types.Position) Range(org.graalvm.tools.lsp.server.types.Range) Source(com.oracle.truffle.api.source.Source) TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)

Example 4 with Range

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);
}
Also used : Range(org.graalvm.tools.lsp.server.types.Range) TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)

Example 5 with Range

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;
}
Also used : StatementTag(com.oracle.truffle.api.instrumentation.StandardTags.StatementTag) LoadSourceSectionEvent(com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) ArrayList(java.util.ArrayList) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) SourceSection(com.oracle.truffle.api.source.SourceSection) Range(org.graalvm.tools.lsp.server.types.Range) LoadSourceSectionListener(com.oracle.truffle.api.instrumentation.LoadSourceSectionListener) HashSet(java.util.HashSet)

Aggregations

Range (org.graalvm.tools.lsp.server.types.Range)5 SourceSection (com.oracle.truffle.api.source.SourceSection)3 TextDocumentContentChangeEvent (org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)3 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 Source (com.oracle.truffle.api.source.Source)2 Position (org.graalvm.tools.lsp.server.types.Position)2 CompilerDirectives (com.oracle.truffle.api.CompilerDirectives)1 TruffleLogger (com.oracle.truffle.api.TruffleLogger)1 LoadSourceSectionEvent (com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent)1 LoadSourceSectionListener (com.oracle.truffle.api.instrumentation.LoadSourceSectionListener)1 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)1 SourcePredicate (com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate)1 StatementTag (com.oracle.truffle.api.instrumentation.StandardTags.StatementTag)1 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1