Skip to content

Commit

Permalink
More json encoding changes, about dialog changes and lsp text sync fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekopalypse committed Oct 26, 2021
1 parent 6d14566 commit 4115b46
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
4 changes: 2 additions & 2 deletions NppLsp.rc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ BEGIN
DEFPUSHBUTTON "Close", IDOK, 86, 109, 50, 14
GROUPBOX "", IDC_VERSION, 10, 9, 201, 93, BS_CENTER
LTEXT "Author: Ekopalypse <[email protected]>", IDC_STATIC, 30, 26, 180, 8
LTEXT "The NppLsp code is hosted on", IDC_STATIC, 30, 43, 180, 8
LTEXT "The NppLspClient code is hosted at", IDC_STATIC, 30, 43, 180, 8
LTEXT "www.github.com/Ekopalypse", IDC_STATIC, 30, 60, 180, 8
LTEXT "This code is licensed under GPLv2", IDC_STATIC, 30, 80, 180, 8
LTEXT "This code is licensed under MIT", IDC_STATIC, 30, 80, 180, 8
CONTROL IDI_LSP, IDC_STATIC,"Static", SS_ICON, 171, 65, 32, 32
END

Expand Down
40 changes: 31 additions & 9 deletions NppLsp.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ __global (
npp notepadpp.Npp
dll_instance voidptr
p Plugin
end_line u32
end_char u32
)

pub struct Plugin {
Expand Down Expand Up @@ -175,15 +177,35 @@ fn be_notified(notification &sci.SCNotification) {

sci.scn_modified {
if p.document_is_of_interest {
mod_type := notification.modification_type & (sci.sc_mod_inserttext | sci.sc_mod_deletetext)
if mod_type > 0 {
text := unsafe { cstring_to_vstring(notification.text)[..int(notification.length)] }
lsp.on_buffer_modified(p.current_file_path,
notification.position,
text,
notification.length,
notification.lines_added,
mod_type == 1)
if notification.modification_type & sci.sc_mod_beforedelete == sci.sc_mod_beforedelete {
end_pos := notification.position + notification.length
end_line = u32(editor.line_from_position(usize(end_pos)))
end_line_start_pos := editor.position_from_line(usize(end_line))
end_char = u32(end_pos - end_line_start_pos)
} else {
mod_type := notification.modification_type & (sci.sc_mod_inserttext | sci.sc_mod_deletetext)
if mod_type > 0 {
start_line := u32(editor.line_from_position(usize(notification.position)))
line_start_pos := editor.position_from_line(usize(start_line))
start_char := u32(notification.position - line_start_pos)
mut range_length := u32(notification.length)
mut content := ''

if mod_type & sci.sc_mod_inserttext == sci.sc_mod_inserttext {
end_line = start_line
end_char = start_char
range_length = 0
content = unsafe { cstring_to_vstring(notification.text)[..int(notification.length)] }
}

lsp.on_buffer_modified(p.current_file_path,
start_line,
start_char,
end_line,
end_char,
range_length,
content)
}
}
}
}
Expand Down
45 changes: 18 additions & 27 deletions lsp/client.v
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ pub fn on_file_closed(file_name string) {
}
}

pub fn on_buffer_modified(file_name string, position isize, text string, length isize, lines_added isize, was_added bool) {
pub fn on_buffer_modified(file_name string,
start_line u32,
start_char_pos u32,
end_line u32,
end_char_pos u32,
range_length u32,
text string) {

if p.lsp_config.lspservers[p.current_language].initialized {
p.current_file_version++

Expand All @@ -98,28 +105,16 @@ pub fn on_buffer_modified(file_name string, position isize, text string, length
)
}
2 { // TextDocumentSyncKind.incremental
mut content := ''
start_line := u32(editor.line_from_position(usize(position)))
start_char := u32(position - editor.position_from_line(usize(start_line)))
mut end_line := u32(0)
mut end_char := start_char

if was_added {
content = text.replace_each(['\\', '\\\\', '\b', r'\b', '\f', r'\f', '\r', r'\r', '\n', r'\n', '\t', r'\t', '"', r'\"'])
end_line = start_line + u32(lines_added)
} else { // deleted
if lines_added < 0 {
end_line = start_line + (u32(lines_added) * -1)
end_char = 0
} else {
end_line = start_line
end_char = start_char + 1
}
}

content := text.replace_each(['\\', '\\\\', '\b', r'\b', '\f', r'\f', '\r', r'\r', '\n', r'\n', '\t', r'\t', '"', r'\"'])
lsp.write_to(
p.current_stdin,
lsp.did_change_incremental(file_name, p.current_file_version, content, start_line, start_char, end_line, end_char)
lsp.did_change_incremental(file_name,
p.current_file_version,
content,
start_line,
start_char_pos,
end_line,
end_char_pos)
)
}
else{}
Expand All @@ -128,19 +123,15 @@ pub fn on_buffer_modified(file_name string, position isize, text string, length
// trigger_characters might be in both lists, hence two if's
// not sure if this is a good idea.
if text in p.lsp_config.lspservers[p.current_language].features.compl_trigger_chars {
line := editor.line_from_position(usize(position))
char_pos := position - editor.position_from_line(usize(line))
lsp.write_to(
p.current_stdin,
lsp.request_completion(file_name, u32(line), u32(char_pos+1), text) // ?? why +1
lsp.request_completion(file_name, start_line, start_char_pos+1, text) // ?? why +1
)
}
if text in p.lsp_config.lspservers[p.current_language].features.sig_help_trigger_chars {
line := editor.line_from_position(usize(position))
char_pos := position - editor.position_from_line(usize(line))
lsp.write_to(
p.current_stdin,
lsp.request_signature_help(file_name, u32(line), u32(char_pos+1), text)
lsp.request_signature_help(file_name, start_line, start_char_pos+1, text)
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions scintilla/scintilla.v
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ fn (e Editor) call(msg int, wparam usize, lparam isize) isize {

pub fn create_editors(main_handle voidptr, second_handle voidptr) Editor {
mut editor := Editor{}
editor.main_func = SCI_FN_DIRECT(send_message(main_handle, 2184, 0, 0))
editor.main_hwnd = voidptr(send_message(main_handle, 2185, 0, 0))
editor.second_func = SCI_FN_DIRECT(send_message(second_handle, 2184, 0, 0))
editor.second_hwnd = voidptr(send_message(second_handle, 2185, 0, 0))
editor.main_func = SCI_FN_DIRECT(send_message(main_handle, sci_getdirectfunction, 0, 0))
editor.main_hwnd = voidptr(send_message(main_handle, sci_getdirectpointer, 0, 0))
editor.second_func = SCI_FN_DIRECT(send_message(second_handle, sci_getdirectfunction, 0, 0))
editor.second_hwnd = voidptr(send_message(second_handle, sci_getdirectpointer, 0, 0))
editor.current_func = editor.main_func
editor.current_hwnd = editor.main_hwnd
return editor
Expand Down

0 comments on commit 4115b46

Please sign in to comment.