Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hanging on windows #130

Merged
merged 8 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.read_file handles relative and absolute paths, like the ones returned by os.real_path .


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
}