Skip to content

Commit

Permalink
feat: Improve code completion and thread safety
Browse files Browse the repository at this point in the history
- Add null check for editor project in documentChanged
- Refactor debounce logic for better readability
- Implement thread-safe access to editor offset
  • Loading branch information
yangbobo2021 committed Nov 8, 2024
1 parent e03f017 commit 3f9925f
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ class EditorListener : EditorFactoryListener {

val documentListener = object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
logger.info("DocumentListener: documentChanged $event")
logger.info("DocumentListener: documentChanged $event, Editor project: ${editor.project}")
if (editor.project != null) {
logger.info("DocumentListener: documentChanged $event")

debouncer.debounce {
ApplicationManager.getApplication().invokeLater({
processDocumentChange(event, editor, editorManager, completionProvider, inlineCompletionService)
}, ModalityState.defaultModalityState())
debouncer.debounce {
processDocumentChange(
event,
editor,
editorManager,
completionProvider,
inlineCompletionService
)
}
}
}
}
Expand Down Expand Up @@ -110,17 +117,22 @@ class EditorListener : EditorFactoryListener {
}
}

var editorOffset: Int = 0
ApplicationManager.getApplication().invokeAndWait({
editorOffset = editor.caretModel.primaryCaret.offset
}, ModalityState.defaultModalityState())

completionProvider.ongoingCompletion.value?.let {
if (it.editor == editor && it.offset == editor.caretModel.primaryCaret.offset) {
if (it.editor == editor && it.offset == editorOffset) {
logger.info("Keeping ongoing completion.")
} else {
logger.info("Cancelling previous completion and providing new one.")
completionProvider.clear()
completionProvider.provideCompletion(editor, editor.caretModel.primaryCaret.offset)
completionProvider.provideCompletion(editor, editorOffset)
}
} ?: run {
logger.info("Providing new completion.")
completionProvider.provideCompletion(editor, editor.caretModel.primaryCaret.offset)
completionProvider.provideCompletion(editor, editorOffset)
}
} else {
logger.debug("Completion is disabled.")
Expand Down

0 comments on commit 3f9925f

Please sign in to comment.