Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
phcreery committed Sep 28, 2024
1 parent 4f93826 commit 38c7e60
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/server/features_formatting.v
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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())
Expand Down
45 changes: 45 additions & 0 deletions src/server/features_formatting_test.v
Original file line number Diff line number Diff line change
@@ -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

}

0 comments on commit 38c7e60

Please sign in to comment.