Skip to content
Snippets Groups Projects
Commit cb47120e authored by dgelessus's avatar dgelessus
Browse files

Change ProBKernel.inspect internals to use PositionedString

The PositionedString offsets here now use the same origin as the "at"
index received from Jupyter. This way the "at" index no longer needs to
be offset manually after the command name is removed from the string.
parent 9f42c8ec
No related branches found
No related tags found
No related merge requests found
...@@ -361,8 +361,8 @@ public final class ProBKernel extends BaseKernel { ...@@ -361,8 +361,8 @@ public final class ProBKernel extends BaseKernel {
} }
} }
private static @Nullable DisplayData inspectCommandArguments(final @NotNull Command command, final @NotNull String argString, final int at) { private static @Nullable DisplayData inspectCommandArguments(final @NotNull Command command, final @NotNull PositionedString argString, final int at) {
final SplitResult split = CommandUtils.splitArgs(command.getParameters(), new PositionedString(argString, 0), at); final SplitResult split = CommandUtils.splitArgs(command.getParameters(), argString, at);
if (split.getParameterAtPosition().isPresent()) { if (split.getParameterAtPosition().isPresent()) {
final Optional<Inspector> inspector = command.getParameterInspectors().getInspectorForParameter(split.getParameterAtPosition().get()); final Optional<Inspector> inspector = command.getParameterInspectors().getInspectorForParameter(split.getParameterAtPosition().get());
if (inspector.isPresent()) { if (inspector.isPresent()) {
...@@ -378,13 +378,10 @@ public final class ProBKernel extends BaseKernel { ...@@ -378,13 +378,10 @@ public final class ProBKernel extends BaseKernel {
} }
} }
@Override private @Nullable DisplayData inspectInternal(final @NotNull PositionedString code, final int at) {
public @Nullable DisplayData inspect(final @NotNull String code, final int at, final boolean extraDetail) { final Matcher commandMatcher = COMMAND_PATTERN.matcher(code.getValue());
// Note: We ignore the extraDetail parameter, because in practice it is always false. This is because the inspect_request messages sent by Jupyter Notebook always have their detail_level set to 0.
final Matcher commandMatcher = COMMAND_PATTERN.matcher(code);
if (commandMatcher.matches()) { if (commandMatcher.matches()) {
// The code is a valid command. // The code is a valid command.
final int argOffset = commandMatcher.start(2);
final String name = commandMatcher.group(1); final String name = commandMatcher.group(1);
if (this.getCommands().containsKey(name)) { if (this.getCommands().containsKey(name)) {
final Command command = this.getCommands().get(name); final Command command = this.getCommands().get(name);
...@@ -397,8 +394,13 @@ public final class ProBKernel extends BaseKernel { ...@@ -397,8 +394,13 @@ public final class ProBKernel extends BaseKernel {
} else { } else {
// The cursor is somewhere in the command arguments, ask the command to inspect. // The cursor is somewhere in the command arguments, ask the command to inspect.
assert name != null; assert name != null;
final String argString = commandMatcher.group(2) == null ? "" : commandMatcher.group(2); final PositionedString argString;
return inspectCommandArguments(command, argString, at - argOffset); if (commandMatcher.group(2) == null) {
argString = code.substring(code.getValue().length());
} else {
argString = code.substring(commandMatcher.start(2), commandMatcher.end(2));
}
return inspectCommandArguments(command, argString, at);
} }
} else { } else {
// Invalid command, can't inspect. // Invalid command, can't inspect.
...@@ -410,6 +412,12 @@ public final class ProBKernel extends BaseKernel { ...@@ -410,6 +412,12 @@ public final class ProBKernel extends BaseKernel {
} }
} }
@Override
public @Nullable DisplayData inspect(final @NotNull String code, final int at, final boolean extraDetail) {
// Note: We ignore the extraDetail parameter, because in practice it is always false. This is because the inspect_request messages sent by Jupyter Notebook always have their detail_level set to 0.
return this.inspectInternal(new PositionedString(code, 0), at);
}
private static @NotNull ReplacementOptions offsetReplacementOptions(final @NotNull ReplacementOptions replacements, final int offset) { private static @NotNull ReplacementOptions offsetReplacementOptions(final @NotNull ReplacementOptions replacements, final int offset) {
return new ReplacementOptions( return new ReplacementOptions(
replacements.getReplacements(), replacements.getReplacements(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment