From 21f562fc101c4e14bf41fac03401d72b8cade2e2 Mon Sep 17 00:00:00 2001 From: Nipuna Ranasinghe Date: Sat, 24 Jun 2023 10:56:06 +0530 Subject: [PATCH 1/2] Fix column calculation during offset to LSP position conversion --- src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java b/src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java index 017b828e..721d5bd9 100644 --- a/src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java +++ b/src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java @@ -113,10 +113,7 @@ public static Position offsetToLSPPos(Editor editor, int offset) { int line = doc.getLineNumber(offset); int lineStart = doc.getLineStartOffset(line); String lineTextBeforeOffset = doc.getText(TextRange.create(lineStart, offset)); - - int tabs = StringUtil.countChars(lineTextBeforeOffset, '\t'); - int tabSize = getTabSize(editor); - int column = lineTextBeforeOffset.length() - tabs * (tabSize - 1); + int column = lineTextBeforeOffset.length(); return new Position(line, column); }); } From f2449aaaabd3dc5927ae09d45afc504ffef1f6c6 Mon Sep 17 00:00:00 2001 From: Nipuna Ranasinghe Date: Sat, 24 Jun 2023 10:58:39 +0530 Subject: [PATCH 2/2] Cleanup document utils --- .../lsp4intellij/utils/DocumentUtils.java | 38 ++----------------- 1 file changed, 3 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java b/src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java index 721d5bd9..d73b4c8e 100644 --- a/src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java +++ b/src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java @@ -27,10 +27,9 @@ import org.eclipse.lsp4j.TextEdit; import org.eclipse.lsp4j.jsonrpc.messages.Either; -import javax.annotation.Nullable; - import java.util.List; import java.util.stream.Collectors; +import javax.annotation.Nullable; import static java.lang.Math.max; import static java.lang.Math.min; @@ -41,37 +40,10 @@ */ public class DocumentUtils { - private static Logger LOG = Logger.getInstance(DocumentUtils.class); + private static final Logger LOG = Logger.getInstance(DocumentUtils.class); public static final String WIN_SEPARATOR = "\r\n"; public static final String LINUX_SEPARATOR = "\n"; - /** - * Gets the line at the given offset given an editor and bolds the text between the given offsets - * - * @param editor The editor - * @param startOffset The starting offset - * @param endOffset The ending offset - * @return The document line - */ - public static String getLineText(Editor editor, int startOffset, int endOffset) { - return computableReadAction(() -> { - Document doc = editor.getDocument(); - int lineIdx = doc.getLineNumber(startOffset); - int lineStartOff = doc.getLineStartOffset(lineIdx); - int lineEndOff = doc.getLineEndOffset(lineIdx); - String line = doc.getText(new TextRange(lineStartOff, lineEndOff)); - int startOffsetInLine = startOffset - lineStartOff; - int endOffsetInLine = endOffset - lineStartOff; - StringBuilder sb = new StringBuilder( line.length()+7 ); - sb.append(line, 0, startOffsetInLine); - sb.append(""); - sb.append(line, startOffsetInLine, endOffsetInLine); - sb.append(""); - sb.append(line, endOffsetInLine, line.length()); - return sb.toString(); - }); - } - /** * Transforms a LogicalPosition (IntelliJ) to an LSP Position * @@ -157,8 +129,6 @@ public static int LSPPosToOffset(Editor editor, Position pos) { return Math.min(max(offset, 0), docLength); }); - - } @Nullable @@ -185,13 +155,11 @@ public static int getTabSize(Editor editor) { return computableReadAction(() -> editor.getSettings().getTabSize(editor.getProject())); } - public static boolean shouldUseSpaces(Editor editor){ + public static boolean shouldUseSpaces(Editor editor) { return computableReadAction(() -> !editor.getSettings().isUseTabCharacter(editor.getProject())); } public static List> toEither(List edits) { return edits.stream().map(Either::forLeft).collect(Collectors.toList()); } - - }