Skip to content

Commit

Permalink
fix hanging on vfmt-ing large files on windows (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
phcreery authored Oct 8, 2024
1 parent bc5b3ca commit ea53875
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
28 changes: 21 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,36 @@ 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.with_fields({
'uri': file.uri.str()
}).info('Formatting file')

mut fmt_proc := ls.launch_tool('fmt', os.norm_path(temp_formatting_file_path))!
defer {
fmt_proc.close()
}
fmt_proc.run()

// read entire output until EOF
mut output := fmt_proc.stdout_slurp()
fmt_proc.wait()

if fmt_proc.code != 0 {
errors := fmt_proc.stderr_slurp().trim_space()
ls.client.show_message(errors, .info)
return error('Formatting failed: ${errors}')
}
loglib.with_fields({
'code': fmt_proc.code.str()
'status': fmt_proc.status.str()
}).info('Formatting finished')

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 []
}

return [
lsp.TextEdit{
range: tform.text_range_to_lsp_range(file.psi_file.root().text_range())
Expand Down
35 changes: 35 additions & 0 deletions src/server/features_formatting_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module server

import lsp
import os
import analyzer
import analyzer.psi
import analyzer.parser

fn test_large_file() {
test_file := os.real_path(@VMODROOT + '/src/server/general.v')

src := os.read_file(test_file) or { panic('Cannot read file') }

mut ls := LanguageServer.new(analyzer.IndexingManager.new())
ls.setup_toolchain()
ls.setup_vpaths()

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)
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') }
assert text_edit_result.len == 1
}

0 comments on commit ea53875

Please sign in to comment.