Search in sources :

Example 1 with SourceBreakpoint

use of org.eclipse.lsp4j.debug.SourceBreakpoint in project magik-tools by StevenLooman.

the class BreakpointManager method addBreakpoint.

/**
 * Add a new breakpoint to self and debugger.
 *
 * @param source                     Source.
 * @param sourceBreakpoint Source breakpoint.
 * @return New magik breakpoint.
 * @throws IOException -
 * @throws ExecutionException -
 * @throws InterruptedException -
 */
MagikBreakpoint addBreakpoint(final Source source, final SourceBreakpoint sourceBreakpoint) throws IOException, InterruptedException, ExecutionException {
    int line = sourceBreakpoint.getLine();
    final AstNode methodNode = this.getNode(source, line);
    final MethodDefinitionNodeHelper helper = new MethodDefinitionNodeHelper(methodNode);
    String method = null;
    int methodLine = 0;
    if (methodNode == null) {
        method = "<not_in_method>";
    } else {
        method = helper.getPakkageExemplarMethodName();
        methodLine = methodNode.getTokenLine();
        if (methodLine == line) {
            line = 0;
        }
    }
    String condition = sourceBreakpoint.getCondition();
    return this.createBreakpoint(source, method, line, condition);
}
Also used : MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) FunctionBreakpoint(org.eclipse.lsp4j.debug.FunctionBreakpoint) SourceBreakpoint(org.eclipse.lsp4j.debug.SourceBreakpoint) AstNode(com.sonar.sslr.api.AstNode)

Example 2 with SourceBreakpoint

use of org.eclipse.lsp4j.debug.SourceBreakpoint in project magik-tools by StevenLooman.

the class BreakpointManager method setBreakpoints.

/**
 * Set breakpoints in source.
 *
 * @param source Source for breakpoints.
 * @param newSourceBreakpoints Breakpoints.
 * @return Current breakpoints.
 * @throws IOException -
 * @throws ExecutionException -
 * @throws InterruptedException -
 */
public List<MagikBreakpoint> setBreakpoints(final Source source, final SourceBreakpoint[] newSourceBreakpoints) throws IOException, InterruptedException, ExecutionException {
    // Get all current breakpoints.
    final List<MagikBreakpoint> breakpoints = this.sourceBreakpoints.computeIfAbsent(source, key -> new ArrayList<>());
    // Add new breakpoints.
    final List<Integer> magikBreakpointLines = breakpoints.stream().map(MagikBreakpoint::getMethodLine).collect(Collectors.toList());
    final List<SourceBreakpoint> addedBreakpoints = Arrays.stream(newSourceBreakpoints).filter(breakpoint -> !magikBreakpointLines.contains(breakpoint.getLine())).collect(Collectors.toList());
    for (final SourceBreakpoint sourceBreakpoint : addedBreakpoints) {
        this.addBreakpoint(source, sourceBreakpoint);
    }
    // Remove old breakpoints.
    final List<Integer> sourceBreakpointLines = Arrays.stream(newSourceBreakpoints).map(SourceBreakpoint::getLine).collect(Collectors.toList());
    final List<MagikBreakpoint> removedBreakpoints = breakpoints.stream().filter(magikBreakpoint -> !sourceBreakpointLines.contains(magikBreakpoint.getMethodLine())).collect(Collectors.toList());
    for (final MagikBreakpoint magikBreakpoint : removedBreakpoints) {
        this.removeBreakpoint(source, magikBreakpoint);
    }
    // breakpoints gets updated through addBreakpoint/removeBreakpoint.
    return breakpoints;
}
Also used : BreakpointEvent(nl.ramsolutions.sw.magik.debugadapter.slap.events.BreakpointEvent) AstNode(com.sonar.sslr.api.AstNode) Arrays(java.util.Arrays) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ISlapResponse(nl.ramsolutions.sw.magik.debugadapter.slap.ISlapResponse) ArrayList(java.util.ArrayList) ISlapProtocol(nl.ramsolutions.sw.magik.debugadapter.slap.ISlapProtocol) Source(org.eclipse.lsp4j.debug.Source) Map(java.util.Map) Position(nl.ramsolutions.sw.magik.analysis.Position) Path(java.nio.file.Path) AstQuery(nl.ramsolutions.sw.magik.analysis.AstQuery) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) EvalResponse(nl.ramsolutions.sw.magik.debugadapter.slap.responses.EvalResponse) Nullable(javax.annotation.Nullable) FunctionBreakpoint(org.eclipse.lsp4j.debug.FunctionBreakpoint) StoppedEventArgumentsReason(org.eclipse.lsp4j.debug.StoppedEventArgumentsReason) Logger(org.slf4j.Logger) MagikParser(nl.ramsolutions.sw.magik.parser.MagikParser) ExceptionBreakpointsFilter(org.eclipse.lsp4j.debug.ExceptionBreakpointsFilter) IDebugProtocolClient(org.eclipse.lsp4j.debug.services.IDebugProtocolClient) IOException(java.io.IOException) SourceBreakpoint(org.eclipse.lsp4j.debug.SourceBreakpoint) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) SlapErrorException(nl.ramsolutions.sw.magik.debugadapter.slap.SlapErrorException) List(java.util.List) BreakpointSetResponse(nl.ramsolutions.sw.magik.debugadapter.slap.responses.BreakpointSetResponse) CheckForNull(javax.annotation.CheckForNull) StoppedEventArguments(org.eclipse.lsp4j.debug.StoppedEventArguments) SourceBreakpoint(org.eclipse.lsp4j.debug.SourceBreakpoint)

Example 3 with SourceBreakpoint

use of org.eclipse.lsp4j.debug.SourceBreakpoint in project magik-tools by StevenLooman.

the class MagikDebugAdapter method setBreakpoints.

@Override
public CompletableFuture<SetBreakpointsResponse> setBreakpoints(final SetBreakpointsArguments args) {
    LOGGER.trace("setBreakpoints");
    return CompletableFuture.supplyAsync(() -> {
        final Source source = args.getSource();
        // Do some manual fixing if needed.
        if (source.getName() == null && source.getPath() != null) {
            final Path path = Path.of(source.getPath());
            final String name = path.getFileName().toString();
            source.setName(name);
        }
        try {
            final SourceBreakpoint[] sourceBreakpoints = args.getBreakpoints();
            final List<MagikBreakpoint> magikBreakpoints = this.breakpointManager.setBreakpoints(source, sourceBreakpoints);
            // Return response.
            final SetBreakpointsResponse response = new SetBreakpointsResponse();
            final Breakpoint[] breakpoints = Lsp4jConversion.toLsp4j(source, magikBreakpoints);
            response.setBreakpoints(breakpoints);
            return response;
        } catch (InterruptedException exception) {
            java.lang.Thread.currentThread().interrupt();
            throw new CompletionException(exception.getMessage(), exception);
        } catch (IOException | ExecutionException exception) {
            throw new CompletionException(exception.getMessage(), exception);
        }
    });
}
Also used : Path(java.nio.file.Path) MagikBreakpoint(nl.ramsolutions.sw.magik.debugadapter.BreakpointManager.MagikBreakpoint) Breakpoint(org.eclipse.lsp4j.debug.Breakpoint) FunctionBreakpoint(org.eclipse.lsp4j.debug.FunctionBreakpoint) SourceBreakpoint(org.eclipse.lsp4j.debug.SourceBreakpoint) MagikBreakpoint(nl.ramsolutions.sw.magik.debugadapter.BreakpointManager.MagikBreakpoint) IOException(java.io.IOException) Source(org.eclipse.lsp4j.debug.Source) SourceBreakpoint(org.eclipse.lsp4j.debug.SourceBreakpoint) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) SetBreakpointsResponse(org.eclipse.lsp4j.debug.SetBreakpointsResponse)

Example 4 with SourceBreakpoint

use of org.eclipse.lsp4j.debug.SourceBreakpoint in project magik-tools by StevenLooman.

the class BreakpointManagerTest method testAddBreakpoint.

@Test
void testAddBreakpoint() throws IOException, InterruptedException, ExecutionException {
    final TestSlapProtocol slapProtocol = new TestSlapProtocol();
    final BreakpointManager manager = new BreakpointManager(slapProtocol, null);
    final SourceBreakpoint sourceBreakpoint = new SourceBreakpoint();
    sourceBreakpoint.setLine(17);
    final Source source = new Source();
    source.setPath(getPath("magik-debug-adapter/src/test/resources/bpt.magik").toString());
    final BreakpointManager.MagikBreakpoint breakpoint = manager.addBreakpoint(source, sourceBreakpoint);
    assertThat(breakpoint.getMethodName()).isEqualTo("user:bpt.t()");
    assertThat(breakpoint.getMethodLine()).isZero();
    assertThat(breakpoint.getCondition()).isNull();
    assertThat(breakpoint.getBreakpointId()).isNotEqualTo(ISlapProtocol.INVALID_BREAKPOINT_ID);
}
Also used : SourceBreakpoint(org.eclipse.lsp4j.debug.SourceBreakpoint) Source(org.eclipse.lsp4j.debug.Source) Test(org.junit.jupiter.api.Test)

Example 5 with SourceBreakpoint

use of org.eclipse.lsp4j.debug.SourceBreakpoint in project magik-tools by StevenLooman.

the class BreakpointManagerTest method testAddBreakpointInvalidLine.

@Test
void testAddBreakpointInvalidLine() throws IOException, InterruptedException, ExecutionException {
    final TestSlapProtocol slapProtocol = new TestSlapProtocol() {

        @Override
        public CompletableFuture<ISlapResponse> setBreakpoint(String method, int line) {
            ErrorResponse errorResponse = new ErrorResponse(RequestType.BREAKPOINT_SET, ErrorMessage.INVALID_LINE_NUMBER);
            SlapErrorException exception = new SlapErrorException(errorResponse);
            CompletableFuture<ISlapResponse> future = new CompletableFuture<>();
            future.completeExceptionally(exception);
            return future;
        }
    };
    final BreakpointManager manager = new BreakpointManager(slapProtocol, null);
    final SourceBreakpoint sourceBreakpoint = new SourceBreakpoint();
    sourceBreakpoint.setLine(18);
    final Source source = new Source();
    source.setPath(getPath("magik-debug-adapter/src/test/resources/bpt.magik").toString());
    final BreakpointManager.MagikBreakpoint breakpoint = manager.addBreakpoint(source, sourceBreakpoint);
    assertThat(breakpoint.getMethodName()).isEqualTo("user:bpt.t()");
    assertThat(breakpoint.getMethodLine()).isEqualTo(18);
    assertThat(breakpoint.getCondition()).isNull();
    assertThat(breakpoint.getMessage()).isEqualTo("INVALID_LINE_NUMBER");
    assertThat(breakpoint.getBreakpointId()).isEqualTo(ISlapProtocol.INVALID_BREAKPOINT_ID);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ISlapResponse(nl.ramsolutions.sw.magik.debugadapter.slap.ISlapResponse) SlapErrorException(nl.ramsolutions.sw.magik.debugadapter.slap.SlapErrorException) SourceBreakpoint(org.eclipse.lsp4j.debug.SourceBreakpoint) Source(org.eclipse.lsp4j.debug.Source) ErrorResponse(nl.ramsolutions.sw.magik.debugadapter.slap.responses.ErrorResponse) Test(org.junit.jupiter.api.Test)

Aggregations

SourceBreakpoint (org.eclipse.lsp4j.debug.SourceBreakpoint)9 Source (org.eclipse.lsp4j.debug.Source)8 CompletableFuture (java.util.concurrent.CompletableFuture)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ISlapResponse (nl.ramsolutions.sw.magik.debugadapter.slap.ISlapResponse)3 SlapErrorException (nl.ramsolutions.sw.magik.debugadapter.slap.SlapErrorException)3 FunctionBreakpoint (org.eclipse.lsp4j.debug.FunctionBreakpoint)3 Test (org.junit.jupiter.api.Test)3 AstNode (com.sonar.sslr.api.AstNode)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ExecutionException (java.util.concurrent.ExecutionException)2 MethodDefinitionNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper)2 ErrorResponse (nl.ramsolutions.sw.magik.debugadapter.slap.responses.ErrorResponse)2 IResource (org.eclipse.core.resources.IResource)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPath (org.eclipse.core.runtime.IPath)2 IBreakpoint (org.eclipse.debug.core.model.IBreakpoint)2 ILineBreakpoint (org.eclipse.debug.core.model.ILineBreakpoint)2