Skip to content

Commit

Permalink
[flow] Enable detailed error rendering only for opened files
Browse files Browse the repository at this point in the history
Reviewed By: panagosg7

Differential Revision: D66847818

fbshipit-source-id: 74726c0a16c848741c2b28ee220a813d34e1384d
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Dec 6, 2024
1 parent 7cd2330 commit 2ff1d3d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
20 changes: 20 additions & 0 deletions newtests/lsp/detailed_diagnostics/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import type {SuiteType} from '../../Tester';
const {readFileSync} = require('fs');
const {join} = require('path');
const {suite, test} = require('../../Tester');

Expand All @@ -24,6 +25,25 @@ module.exports = (suite(
initializationOptions: {detailedErrorRendering: true},
}),
addFile('file_with_simple_error.js')
.waitUntilLSPMessage(
9000,
'textDocument/publishDiagnostics',
'{Cannot assign}',
)
.verifyLSPMessageSnapshot(
join(__dirname, '__snapshots__', 'no-detailed-errors.json'),
['window/showStatus', '$/cancelRequest'],
),
lspNotification('textDocument/didOpen', {
textDocument: {
uri: '<PLACEHOLDER_PROJECT_URL>/file_with_simple_error.js',
languageId: 'javascript',
version: 1,
text: readFileSync(
join(__dirname, 'file_with_simple_error.js'),
).toString(),
},
})
.waitUntilLSPMessage(
9000,
'textDocument/publishDiagnostics',
Expand Down
10 changes: 6 additions & 4 deletions src/common/flow_lsp_conversions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ let position_of_document_position { Lsp.TextDocumentPositionParams.position; _ }
let diagnostics_of_flow_errors =
let error_to_lsp
~unsaved_content
~vscode_detailed_diagnostics
~should_include_vscode_detailed_diagnostics
~(severity : Severity.severity)
(printable_error : Loc.t Flow_errors_utils.printable_error) :
(Lsp.DocumentUri.t * Lsp.PublishDiagnostics.diagnostic) option =
Expand All @@ -253,7 +253,7 @@ let diagnostics_of_flow_errors =
~f:related_to_lsp
in
let data =
if vscode_detailed_diagnostics then
if should_include_vscode_detailed_diagnostics printable_error then
let map_color = function
| Tty.Default -> Lsp.PublishDiagnostics.ExtraDetailedDiagnosticV0.Default
| Tty.Black -> Lsp.PublishDiagnostics.ExtraDetailedDiagnosticV0.Black
Expand Down Expand Up @@ -306,9 +306,11 @@ let diagnostics_of_flow_errors =
)
| Error _ -> None
in
fun ~unsaved_content ~vscode_detailed_diagnostics ~errors ~warnings ->
fun ~unsaved_content ~should_include_vscode_detailed_diagnostics ~errors ~warnings ->
let add severity error acc =
match error_to_lsp ~unsaved_content ~vscode_detailed_diagnostics ~severity error with
match
error_to_lsp ~unsaved_content ~should_include_vscode_detailed_diagnostics ~severity error
with
| Some (uri, diagnostic) -> Lsp.UriMap.add ~combine:List.append uri [diagnostic] acc
| None -> acc
in
Expand Down
3 changes: 2 additions & 1 deletion src/server/command_handler/commandHandler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3601,7 +3601,8 @@ let handle_live_errors_request =
let live_diagnostics =
Flow_lsp_conversions.diagnostics_of_flow_errors
~unsaved_content:(Some (File_path.make file_path, content))
~vscode_detailed_diagnostics:(vscode_detailed_diagnostics client)
~should_include_vscode_detailed_diagnostics:
(Base.Fn.const (vscode_detailed_diagnostics client))
~errors:live_errors
~warnings:live_warnings
|> Lsp.UriMap.find_opt live_errors_uri
Expand Down
12 changes: 11 additions & 1 deletion src/server/persistent_connection/persistent_connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,20 @@ let send_errors =
Lsp.Initialize.(client.lsp_initialize_params.initializationOptions.detailedErrorRendering)
~default:false
in
let should_include_vscode_detailed_diagnostics =
if vscode_detailed_diagnostics then
fun error ->
let open Flow_errors_utils in
match loc_of_printable_error error |> Loc.source with
| None -> false
| Some source -> SMap.mem (File_key.to_string source) client.opened_files
else
Base.Fn.const false
in
let diagnostics =
Flow_lsp_conversions.diagnostics_of_flow_errors
~unsaved_content:None
~vscode_detailed_diagnostics
~should_include_vscode_detailed_diagnostics
~errors
~warnings
in
Expand Down

0 comments on commit 2ff1d3d

Please sign in to comment.