From 1088f7ba12b56123d25d58a6805161c12bf768b7 Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Tue, 16 Jun 2020 21:56:06 +0200 Subject: [PATCH] Add PositionedString.getEndPosition helper method --- src/main/java/de/prob2/jupyter/CommandUtils.java | 2 +- src/main/java/de/prob2/jupyter/PositionedString.java | 10 ++++++++++ src/main/java/de/prob2/jupyter/ProBKernel.java | 6 +++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/prob2/jupyter/CommandUtils.java b/src/main/java/de/prob2/jupyter/CommandUtils.java index cf09dee..e7de84b 100644 --- a/src/main/java/de/prob2/jupyter/CommandUtils.java +++ b/src/main/java/de/prob2/jupyter/CommandUtils.java @@ -166,7 +166,7 @@ public final class CommandUtils { * @return the result of the split operation (see {@link SplitResult} for details) */ public static @NotNull SplitResult splitArgs(final @NotNull Parameters parameters, final @NotNull PositionedString argString) { - return splitArgs(parameters, argString, argString.getStartPosition() + argString.getValue().length()); + return splitArgs(parameters, argString, argString.getEndPosition()); } private static <T> void validateSplitParameter(final @NotNull ParsedArguments parsed, final @NotNull SplitArguments splitArgs, final @NotNull Parameter<T> param) { diff --git a/src/main/java/de/prob2/jupyter/PositionedString.java b/src/main/java/de/prob2/jupyter/PositionedString.java index ba32db9..26a14c3 100644 --- a/src/main/java/de/prob2/jupyter/PositionedString.java +++ b/src/main/java/de/prob2/jupyter/PositionedString.java @@ -37,6 +37,16 @@ public final class PositionedString { return this.startPosition; } + /** + * Return the position of the end of this string. + * This is equivalent to the start position plus the length of the string. + * + * @return the position of the end of this string + */ + public int getEndPosition() { + return this.getStartPosition() + this.getValue().length(); + } + /** * <p> * Return a substring of this string, with the position adjusted accordingly. diff --git a/src/main/java/de/prob2/jupyter/ProBKernel.java b/src/main/java/de/prob2/jupyter/ProBKernel.java index 70e1fa6..8f7f26a 100644 --- a/src/main/java/de/prob2/jupyter/ProBKernel.java +++ b/src/main/java/de/prob2/jupyter/ProBKernel.java @@ -440,7 +440,7 @@ public final class ProBKernel extends BaseKernel { final SplitCommandCall split = splitCommand(preprocessInput(code)); if (this.getCommands().containsKey(split.getName().getValue())) { final Command command = this.getCommands().get(split.getName().getValue()); - if (at <= split.getName().getStartPosition() + split.getName().getValue().length()) { + if (at <= split.getName().getEndPosition()) { // The cursor is somewhere in the command name, show help text for the command. return command.renderHelp(); } else if (at < split.getArgString().getStartPosition()) { @@ -490,13 +490,13 @@ public final class ProBKernel extends BaseKernel { private @Nullable ReplacementOptions completeInternal(final @NotNull PositionedString code, final int at) { final SplitCommandCall split = splitCommand(preprocessInput(code)); - if (at <= split.getName().getStartPosition() + split.getName().getValue().length()) { + if (at <= split.getName().getEndPosition()) { // The cursor is somewhere in the command name, provide command completions. final String prefix = split.getName().substring(0, at - split.getName().getStartPosition()).getValue(); return new ReplacementOptions( this.getCommands().keySet().stream().filter(s -> s.startsWith(prefix)).sorted().collect(Collectors.toList()), split.getName().getStartPosition(), - split.getName().getStartPosition() + split.getName().getValue().length() + split.getName().getEndPosition() ); } else if (at < split.getArgString().getStartPosition()) { // The cursor is in the whitespace between the command name and arguments, don't show anything. -- GitLab