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: lsp bugs #579

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

package io.github.rosemoe.sora.lsp.editor.completion

import android.util.Log
import io.github.rosemoe.sora.lang.completion.CompletionItemKind
import io.github.rosemoe.sora.lang.completion.SimpleCompletionIconDrawer.draw
import io.github.rosemoe.sora.lang.completion.snippet.parser.CodeSnippetParser
import io.github.rosemoe.sora.lsp.editor.LspEventManager
import io.github.rosemoe.sora.lsp.events.EventType
import io.github.rosemoe.sora.lsp.events.document.applyEdits
import io.github.rosemoe.sora.lsp.utils.OtherUtils
import io.github.rosemoe.sora.lsp.utils.asLspPosition
import io.github.rosemoe.sora.lsp.utils.createPosition
import io.github.rosemoe.sora.lsp.utils.createRange
Expand Down Expand Up @@ -116,13 +118,21 @@ class LspCompletionItem(
val codeSnippet = CodeSnippetParser.parse(textEdit.newText)
val startIndex =
text.getCharIndex(textEdit.range.start.line, textEdit.range.start.character)
val endIndex = text.getCharIndex(textEdit.range.end.line, textEdit.range.end.character)
val selectedText = text.subSequence(startIndex, endIndex).toString()
text.delete(startIndex, endIndex)

editor.snippetController
.startSnippet(startIndex, codeSnippet, selectedText)

var endIndex = 0;
try {
endIndex = text.getCharIndex(textEdit.range.end.line, textEdit.range.end.character)
} catch (e:StringIndexOutOfBoundsException) {
//fix StringIndexOutOfBoundsException
OtherUtils.handleOutOfBoundIndex(e,editor)
endIndex = text.getCharIndex(textEdit.range.end.line, textEdit.range.end.character)
}

if (startIndex < endIndex) {
val selectedText = text.subSequence(startIndex, endIndex).toString()
text.delete(startIndex, endIndex)
editor.snippetController
.startSnippet(startIndex, codeSnippet, selectedText)
}
} else {
eventManager.emit(EventType.applyEdits) {
put("edits", listOf(finalTextEdit))
Expand All @@ -133,7 +143,7 @@ class LspCompletionItem(

if (completionItem.additionalTextEdits != null) {
eventManager.emit(EventType.applyEdits) {
put("edits", listOf(completionItem.additionalTextEdits))
put("edits", completionItem.additionalTextEdits)
put(text)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ open class SignatureHelpWindow(editor: CodeEditor) : EditorPopupWindow(

open fun show(signatureHelp: SignatureHelp) {
this.signatureHelp = signatureHelp
renderSignatureHelp()
updateWindowSizeAndLocation()
show()

if (signatureHelp.activeSignature != null && signatureHelp.activeParameter != null) {
renderSignatureHelp()
updateWindowSizeAndLocation()
show()
} else {
Log.d("SignatureHelpWindow", "activeSignature or activeParameter is null")
return
}

}


Expand Down Expand Up @@ -132,6 +139,7 @@ open class SignatureHelpWindow(editor: CodeEditor) : EditorPopupWindow(

val activeSignatureIndex = signatureHelp.activeSignature
val activeParameterIndex = signatureHelp.activeParameter

val signatures = signatureHelp.signatures

val renderStringBuilder = SpannableStringBuilder()
Expand All @@ -145,6 +153,8 @@ open class SignatureHelpWindow(editor: CodeEditor) : EditorPopupWindow(
Log.d("SignatureHelpWindow", "activeSignature is out of range")
return
}



// Get only the activated signature
for (i in 0..activeSignatureIndex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.github.rosemoe.sora.lsp.utils;

import android.annotation.SuppressLint;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.lsp4j.CompletionItem;

import io.github.rosemoe.sora.widget.CodeEditor;

public class OtherUtils {
@SuppressLint("NewApi")
public static String toString(List<?> list) {
return Arrays
.toString(list
.stream()
.map(Object::toString)
.collect(java.util.stream.Collectors.toList())
.toArray(new String[0]));
}
@SuppressLint("NewApi")
public static String compeltionItemListToString(List<CompletionItem> list) {
return Arrays
.toString(list
.stream()
.map(completionItem -> {
StringBuilder str = new StringBuilder("detail:" + completionItem.getDetail() + "\n");
str.append("label:" +completionItem.getLabel() + '\n');
str.append("insertText:" + completionItem.getInsertText() + "\n");
str.append("data:" + completionItem.getData() + "\n");
return str.toString();
})
.collect(java.util.stream.Collectors.toList())
.toArray(new String[0]));
}
@SuppressLint("NewApi")
public static <K,V> K getKeyClass(Map<K,V> map, V value) {
for (K key: map.keySet()) {
if (map.get(key).getClass() == value.getClass()) return key;
}
return null;
}

public static int handleOutOfBoundIndex(StringIndexOutOfBoundsException e, CodeEditor editor) {
//example : e.getMessage() = "Column 15 out of bounds. line: 15 , column count (line separator included):14"
List<Integer> numbers = new ArrayList<>();
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(e.getMessage());

while (matcher.find()) {
numbers.add(Integer.parseInt(matcher.group()));
}
Log.i("out",String.format("message:%s,result:%s",e.getMessage(),toString(numbers)));
int size = numbers.get(0) - numbers.get(2);
for (int i = 0; i <size ; i++) {
Log.i("out","insert space");
editor.getText().insert(editor.getCursor().getRange().getEnd().getLine(),
editor.getCursor().getRange().getEnd().getColumn(),
" ");
}
editor.getText().insert(editor.getCursor().getRange().getEnd().getLine(),
editor.getCursor().getRange().getEnd().getColumn(),
"\n");

return numbers.get(0) //out bounds column count
- numbers.get(2); //column count
}
}