Skip to content

Commit

Permalink
Added debug infos, some lsp fixes and new lsp feature textDocument/fo…
Browse files Browse the repository at this point in the history
…rmatting
  • Loading branch information
Ekopalypse committed Oct 27, 2021
1 parent 4115b46 commit 4a1f458
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 20 deletions.
59 changes: 52 additions & 7 deletions NppLsp.v
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,16 @@ fn get_funcs_array(mut nb_func &int) &FuncItem {
'Restart server for current language': restart_lsp_server
'Stop all configured lsp server': stop_all_server
'-': voidptr(0)
'Format document': format_document
'Goto definition': goto_definition
'Peek definition': peek_definition
'Goto implementation': goto_implementation
'Peek implementation': peek_implementation
'--': voidptr(0)
'toggle_console': toggle_console
'Open configuration file': open_config
'Apply current configuration': apply_config
'--': voidptr(0)
'---': voidptr(0)
'About': about
}

Expand All @@ -265,8 +271,10 @@ fn get_funcs_array(mut nb_func &int) &FuncItem {
}

fn check_lexer(buffer_id u64) {
p.console_window.log('checking current lexer', 0)
p.current_language = npp.get_language_name_from_id(buffer_id)
p.document_is_of_interest = p.current_language in p.lsp_config.lspservers
p.console_window.log('', 0)
check_ls_status(true)
}

Expand All @@ -276,6 +284,7 @@ pub fn apply_config() {
}

fn read_main_config() {
p.console_window.log('rereading main config', 0)
p.lsp_config = lsp.decode_config(p.main_config_file)
}

Expand All @@ -289,6 +298,7 @@ pub fn open_config() {
}

pub fn start_lsp_server() {
p.console_window.log('starting language server: ${p.current_language}', 0)
check_ls_status(false)
// create and send a fake nppn_bufferactivated event
mut sci_header := sci.SCNotification{text: &char(0)}
Expand All @@ -300,18 +310,21 @@ pub fn start_lsp_server() {
}

pub fn stop_lsp_server() {
p.console_window.log('stopping language server: ${p.current_language}', 0)
p.proc_manager.stop(p.current_language)
p.lsp_config.lspservers[p.current_language].initialized = false
p.console_window.log('initialized = ${p.lsp_config.lspservers[p.current_language].initialized}', 0)
p.current_file_version = 0
}

pub fn restart_lsp_server() {
p.console_window.log('restarting lsp server: ${p.current_language}', 0)
stop_lsp_server()
start_lsp_server()
}

pub fn stop_all_server() {
p.console_window.log('stop all running language server', 0)
p.proc_manager.stop_all_running_processes()
}

Expand All @@ -329,30 +342,62 @@ pub fn about() {
}

fn check_ls_status(check_auto_start bool) {

p.console_window.log('checking language server status: ${p.current_language}', 0)
if p.current_language in p.proc_manager.running_processes {
p.console_window.log(' is already running', 0)
p.cur_lang_srv_running = true
return
}

if check_auto_start && !p.lsp_config.lspservers[p.current_language].auto_start_server {
p.console_window.log(' either unknown language or server should not be started automatically', 0)
p.cur_lang_srv_running = false
return
}


p.console_window.log(' trying to start ${p.lsp_config.lspservers[p.current_language].executable}', 0)
proc_status := p.proc_manager.start(p.current_language,
p.lsp_config.lspservers[p.current_language].executable,
p.lsp_config.lspservers[p.current_language].args.join(' '))

if proc_status == .running {

match proc_status {
.running {
p.console_window.log(' running', 0)
p.console_window.log('${p.current_language} server is running', 0)
p.cur_lang_srv_running = true
p.current_stdin = p.proc_manager.running_processes[p.current_language].stdin
} else {
p.cur_lang_srv_running = false
}
.error_no_executable {
p.console_window.log(' cannot find executable', 0)
p.cur_lang_srv_running = false
}
.failed_to_start {
p.console_window.log(' failed to start', 0)
p.cur_lang_srv_running = false
}
}
}

pub fn format_document() {
lsp.on_format_document(p.current_file_path)
}

pub fn goto_definition() {
lsp.on_goto_definition(p.current_file_path)
}

pub fn peek_definition() {
lsp.on_peek_definition(p.current_file_path)
}

pub fn goto_implementation() {
lsp.on_goto_implementation(p.current_file_path)
}

pub fn peek_implementation() {
lsp.on_peek_implementation(p.current_file_path)
}

[windows_stdcall]
[export: DllMain]
fn main(hinst voidptr, fdw_reason int, lp_reserved voidptr) bool{
Expand Down
67 changes: 65 additions & 2 deletions lsp/client.v
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,49 @@ pub fn on_buffer_modified(file_name string,
}
}

pub fn on_format_document(file_name string) {
if p.lsp_config.lspservers[p.current_language].initialized {
lsp.write_to(
p.current_stdin,
lsp.format_document(file_name, editor.get_tab_size(), editor.use_spaces(), true, true, true)
)
}
}

pub fn on_goto_definition(file_name string) {
if p.lsp_config.lspservers[p.current_language].initialized {
current_pos := editor.get_current_position()
current_line := editor.line_from_position(usize(current_pos))
line_start_pos := editor.position_from_line(usize(current_line))
char_pos := current_pos - line_start_pos
lsp.write_to(
p.current_stdin,
lsp.goto_definition(file_name, u32(current_line), u32(char_pos))
)
}
}

pub fn on_goto_implementation(file_name string) {
if p.lsp_config.lspservers[p.current_language].initialized {
current_pos := editor.get_current_position()
current_line := editor.line_from_position(usize(current_pos))
line_start_pos := editor.position_from_line(usize(current_line))
char_pos := current_pos - line_start_pos
lsp.write_to(
p.current_stdin,
lsp.goto_implementation(file_name, u32(current_line), u32(char_pos))
)
}
}

pub fn on_peek_definition(file_name string) {
p.console_window.log('on_peek_definition not implemented yet', 3)
}

pub fn on_peek_implementation(file_name string) {
p.console_window.log('on_peek_implementation not implemented yet', 3)
}

fn notification_handler(json_message JsonMessage) {
match json_message.method {
'textDocument/publishDiagnostics' {
Expand Down Expand Up @@ -203,7 +246,7 @@ fn initialize_msg_response(json_message string) {
}
}

fn request_completion_response(json_message string) {
fn completion_response(json_message string) {
cl := json2.decode<CompletionList>(json_message) or { CompletionList{} }
mut ci := []CompletionItem{}
if cl.items.len != 0 {
Expand All @@ -215,11 +258,31 @@ fn request_completion_response(json_message string) {
if ci.len > 0 { editor.display_completion_list(ci.map(it.label).join('\n')) }
}

fn request_signature_help_repsonse(json_message string) {
fn signature_help_repsonse(json_message string) {
p.console_window.log(' signature help response received: $json_message', 0)
sh := json2.decode<SignatureHelp>(json_message) or { SignatureHelp{} }
if sh.signatures.len > 0 {
editor.display_signature_hints(sh.signatures[0].label)
}
p.console_window.log('$sh', 0)
}

fn format_document_repsonse(json_message string) {
p.console_window.log(' format document response received: $json_message', 0)
tea := json2.decode<TextEditArray>(json_message) or { TextEditArray{} }
editor.begin_undo_action()
for item in tea.items {
start_pos := u32(editor.position_from_line(usize(item.range.start.line))) + item.range.start.character
end_pos := u32(editor.position_from_line(usize(item.range.end.line))) + item.range.end.character
editor.replace_target(start_pos, end_pos, item.new_text)
}
editor.end_undo_action()
}

fn goto_definition_repsonse(json_message string) {
p.console_window.log('NOT IMPLEMENTED YET: goto definition response received: $json_message', 3)
}

fn goto_implementation_repsonse(json_message string) {
p.console_window.log('NOT IMPLEMENTED YET: goto implementation response received: $json_message', 3)
}
Loading

0 comments on commit 4a1f458

Please sign in to comment.