diff --git a/src/server/features_formatting.v b/src/server/features_formatting.v index 44bdfc4..13cedcb 100644 --- a/src/server/features_formatting.v +++ b/src/server/features_formatting.v @@ -3,6 +3,7 @@ module server import lsp import os import server.tform +import loglib const temp_formatting_file_path = os.join_path(os.temp_dir(), 'v-analyzer-formatting-temp.v') @@ -14,23 +15,30 @@ pub fn (mut ls LanguageServer) formatting(params lsp.DocumentFormattingParams) ! return error('Cannot write temp file for formatting: ${err}') } - mut fmt_proc := ls.launch_tool('fmt', temp_formatting_file_path)! + loglib.info('Formatting file: ${temp_formatting_file_path} ${file.uri}') + + mut fmt_proc := ls.launch_tool('fmt', os.norm_path(temp_formatting_file_path)) or { return [] } defer { fmt_proc.close() } - fmt_proc.wait() + fmt_proc.run() - if fmt_proc.code != 0 { - errors := fmt_proc.stderr_slurp().trim_space() - ls.client.show_message(errors, .info) - return error('Formatting failed: ${errors}') - } + loglib.info('Formatting finished with code: ${fmt_proc.code}') + // read entire output until EOF mut output := fmt_proc.stdout_slurp() + $if windows { output = output.replace('\r\r', '\r') } + if (fmt_proc.code != 0) && (fmt_proc.status == .exited) { + errors := fmt_proc.stderr_slurp().trim_space() + ls.client.show_message(errors, .info) + return error('Formatting failed: ${errors}') + } + + return [ lsp.TextEdit{ range: tform.text_range_to_lsp_range(file.psi_file.root().text_range()) diff --git a/src/server/features_formatting_test.v b/src/server/features_formatting_test.v new file mode 100644 index 0000000..908cb68 --- /dev/null +++ b/src/server/features_formatting_test.v @@ -0,0 +1,45 @@ +module server + +import lsp +import os +import analyzer +import analyzer.psi +import analyzer.parser + + + + +fn test_large_file() { + + + test_file := os.norm_path(@VMODROOT + '/src/server/general.v') + // test_file := os.norm_path(@VEXEROOT + '/vlib/os/process_windows.c.v') + // test_file := 'file:///c%3A/Users/phcre/Documents/v/imageeditor/testing/proc.v' + dump(test_file) + + src := os.read_file(test_file) or { panic('Cannot read file') } + // dump(src) + + uri := lsp.document_uri_from_path(test_file) + res := parser.parse_code(src) + psi_file := psi.new_psi_file(uri.path(), res.tree, res.source_text) + + + mut ls := LanguageServer.new(analyzer.IndexingManager.new()) + ls.opened_files[uri] = analyzer.OpenedFile{ + uri: uri + version: 0 + psi_file: psi_file + } + + params := lsp.DocumentFormattingParams{ + text_document: lsp.TextDocumentIdentifier{ + uri: uri + } + } + + text_edit_result := ls.formatting(params) or { panic('Cannot format file') } + dump(text_edit_result) + assert text_edit_result.len == 1 + +}