Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix column calculation during offset to LSP position conversion #323

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 4 additions & 39 deletions src/main/java/org/wso2/lsp4intellij/utils/DocumentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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("<b>");
sb.append(line, startOffsetInLine, endOffsetInLine);
sb.append("</b>");
sb.append(line, endOffsetInLine, line.length());
return sb.toString();
});
}

/**
* Transforms a LogicalPosition (IntelliJ) to an LSP Position
*
Expand Down Expand Up @@ -113,10 +85,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);
});
}
Expand Down Expand Up @@ -160,8 +129,6 @@ public static int LSPPosToOffset(Editor editor, Position pos) {
return Math.min(max(offset, 0), docLength);

});


}

@Nullable
Expand All @@ -188,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<Either<TextEdit, InsertReplaceEdit>> toEither(List<TextEdit> edits) {
return edits.stream().map(Either::<TextEdit, InsertReplaceEdit>forLeft).collect(Collectors.toList());
}


}