diff --git a/plugin/vscode/quick-lint-js/vscode/qljs-document.cpp b/plugin/vscode/quick-lint-js/vscode/qljs-document.cpp index 94d0b4b7f9..bbbe6fe10f 100644 --- a/plugin/vscode/quick-lint-js/vscode/qljs-document.cpp +++ b/plugin/vscode/quick-lint-js/vscode/qljs-document.cpp @@ -22,7 +22,7 @@ void QLJS_Config_Document::after_modification(::Napi::Env env, void QLJS_Lintable_Document::after_modification( ::Napi::Env env, QLJS_Workspace& workspace, VSCode_Diagnostic_Collection diagnostic_collection) { - this->lint_javascript_and_publish_diagnostics(env, workspace.vscode_, + this->lint_javascript_and_publish_diagnostics(env, workspace, diagnostic_collection); } @@ -61,10 +61,10 @@ void QLJS_Config_Document::finish_init( auto loaded_config_result = workspace.config_loader_.watch_and_load_config_file(*file_path, this); if (loaded_config_result.ok()) { + this->loaded_config_ = *loaded_config_result; workspace.vscode_.load_non_persistent(env); - this->lint_config_and_publish_diagnostics(env, workspace.vscode_, - workspace.diagnostic_collection(), - *loaded_config_result); + this->lint_config_and_publish_diagnostics( + env, workspace, workspace.diagnostic_collection()); } else { QLJS_UNIMPLEMENTED(); } @@ -74,16 +74,26 @@ void QLJS_Config_Document::on_config_file_changed( ::Napi::Env env, QLJS_Workspace& workspace, VSCode_Diagnostic_Collection diagnostic_collection, Loaded_Config_File* config_file) { - this->lint_config_and_publish_diagnostics(env, workspace.vscode_, - diagnostic_collection, config_file); + this->loaded_config_ = config_file; + this->lint_config_and_publish_diagnostics(env, workspace, + diagnostic_collection); } void QLJS_Config_Document::lint_config_and_publish_diagnostics( - ::Napi::Env env, VSCode_Module& vscode, - VSCode_Diagnostic_Collection diagnostic_collection, - Loaded_Config_File* loaded_config) { - diagnostic_collection.set(this->uri(), - this->lint_config(env, &vscode, loaded_config)); + ::Napi::Env env, QLJS_Workspace& workspace, + VSCode_Diagnostic_Collection diagnostic_collection) { + diagnostic_collection.set(this->uri(), this->lint_config(env, workspace)); +} + +::Napi::Array QLJS_Config_Document::lint_config(::Napi::Env env, + QLJS_Workspace& workspace) { + workspace.vscode_.load_non_persistent(env); + + LSP_Locator locator(&this->loaded_config_->file_content); + VSCode_Diag_Reporter diag_reporter(&workspace.vscode_, env, &locator, + this->uri(), workspace.translator_); + diag_reporter.report(this->loaded_config_->errors); + return std::move(diag_reporter).diagnostics(); } void QLJS_Lintable_Document::on_config_file_changed( @@ -92,14 +102,30 @@ void QLJS_Lintable_Document::on_config_file_changed( Loaded_Config_File* config_file) { this->config_ = config_file ? &config_file->config : &workspace.default_config_; - this->lint_javascript_and_publish_diagnostics(env, workspace.vscode_, + this->lint_javascript_and_publish_diagnostics(env, workspace, diagnostic_collection); } void QLJS_Lintable_Document::lint_javascript_and_publish_diagnostics( - ::Napi::Env env, VSCode_Module& vscode, + ::Napi::Env env, QLJS_Workspace& workspace, VSCode_Diagnostic_Collection diagnostic_collection) { - diagnostic_collection.set(this->uri(), this->lint_javascript(env, &vscode)); + diagnostic_collection.set(this->uri(), this->lint_javascript(env, workspace)); +} + +::Napi::Array QLJS_Lintable_Document::lint_javascript( + ::Napi::Env env, QLJS_Workspace& workspace) { + VSCode_Module& vscode = workspace.vscode_; + vscode.load_non_persistent(env); + + VSCode_Diag_Reporter diag_reporter(&vscode, env, &this->document_.locator(), + this->uri(), workspace.translator_); + parse_and_lint(this->document_.string(), diag_reporter, + Linter_Options{ + .language = this->language_, + .configuration = this->config_, + }); + + return std::move(diag_reporter).diagnostics(); } } diff --git a/plugin/vscode/quick-lint-js/vscode/qljs-document.h b/plugin/vscode/quick-lint-js/vscode/qljs-document.h index 79ce2be877..d3150150af 100644 --- a/plugin/vscode/quick-lint-js/vscode/qljs-document.h +++ b/plugin/vscode/quick-lint-js/vscode/qljs-document.h @@ -120,19 +120,12 @@ class QLJS_Config_Document : public QLJS_Document_Base { Loaded_Config_File* config_file) override; private: - void lint_config_and_publish_diagnostics(::Napi::Env, VSCode_Module&, - VSCode_Diagnostic_Collection, - Loaded_Config_File* loaded_config); - - ::Napi::Array lint_config(::Napi::Env env, VSCode_Module* vscode, - Loaded_Config_File* loaded_config) { - vscode->load_non_persistent(env); - - LSP_Locator locator(&loaded_config->file_content); - VSCode_Diag_Reporter diag_reporter(vscode, env, &locator, this->uri()); - diag_reporter.report(loaded_config->errors); - return std::move(diag_reporter).diagnostics(); - } + void lint_config_and_publish_diagnostics(::Napi::Env, QLJS_Workspace&, + VSCode_Diagnostic_Collection); + + ::Napi::Array lint_config(::Napi::Env env, QLJS_Workspace& workspace); + + Loaded_Config_File* loaded_config_ = nullptr; }; class QLJS_Lintable_Document : public QLJS_Document_Base { @@ -150,23 +143,11 @@ class QLJS_Lintable_Document : public QLJS_Document_Base { VSCode_Diagnostic_Collection, Loaded_Config_File* config_file) override; - void lint_javascript_and_publish_diagnostics(::Napi::Env, VSCode_Module&, + void lint_javascript_and_publish_diagnostics(::Napi::Env, QLJS_Workspace&, VSCode_Diagnostic_Collection); private: - ::Napi::Array lint_javascript(::Napi::Env env, VSCode_Module* vscode) { - vscode->load_non_persistent(env); - - VSCode_Diag_Reporter diag_reporter(vscode, env, &this->document_.locator(), - this->uri()); - parse_and_lint(this->document_.string(), diag_reporter, - Linter_Options{ - .language = this->language_, - .configuration = this->config_, - }); - - return std::move(diag_reporter).diagnostics(); - } + ::Napi::Array lint_javascript(::Napi::Env env, QLJS_Workspace& workspace); Configuration* config_; // Initialized by finish_init. diff --git a/plugin/vscode/quick-lint-js/vscode/qljs-workspace.h b/plugin/vscode/quick-lint-js/vscode/qljs-workspace.h index 43f15ddd19..b02f6dbb66 100644 --- a/plugin/vscode/quick-lint-js/vscode/qljs-workspace.h +++ b/plugin/vscode/quick-lint-js/vscode/qljs-workspace.h @@ -248,6 +248,8 @@ class QLJS_Workspace : public ::Napi::ObjectWrap { QLJS_Workspace* workspace_; }; + private: + Translator translator_; bool disposed_ = false; VSCode_Tracer tracer_; VSCode_Module vscode_; diff --git a/plugin/vscode/quick-lint-js/vscode/vscode-diag-reporter.h b/plugin/vscode/quick-lint-js/vscode/vscode-diag-reporter.h index fb32c15e8f..e9e9c3c2da 100644 --- a/plugin/vscode/quick-lint-js/vscode/vscode-diag-reporter.h +++ b/plugin/vscode/quick-lint-js/vscode/vscode-diag-reporter.h @@ -18,8 +18,9 @@ class VSCode_Diag_Formatter explicit VSCode_Diag_Formatter(VSCode_Module* vscode, ::Napi::Env env, ::Napi::Array diagnostics, const LSP_Locator* locator, - ::Napi::Value document_uri) - : Diagnostic_Formatter(qljs_messages), + ::Napi::Value document_uri, + Translator message_translator) + : Diagnostic_Formatter(message_translator), vscode_(vscode), env_(env), diagnostics_(diagnostics), @@ -129,12 +130,14 @@ class VSCode_Diag_Reporter final : public Diag_Reporter { public: explicit VSCode_Diag_Reporter(VSCode_Module* vscode, ::Napi::Env env, const LSP_Locator* locator, - ::Napi::Value document_uri) + ::Napi::Value document_uri, + Translator message_translator) : vscode_(vscode), env_(env), diagnostics_(::Napi::Array::New(env)), locator_(locator), - document_uri_(document_uri) {} + document_uri_(document_uri), + message_translator_(message_translator) {} ::Napi::Array diagnostics() const { return this->diagnostics_; } @@ -144,7 +147,8 @@ class VSCode_Diag_Reporter final : public Diag_Reporter { /*env=*/this->env_, /*diagnostics=*/this->diagnostics_, /*locator=*/this->locator_, - /*document_uri=*/this->document_uri_); + /*document_uri=*/this->document_uri_, + /*message_translator=*/this->message_translator_); formatter.format(get_diagnostic_info(type), diag); } @@ -154,6 +158,7 @@ class VSCode_Diag_Reporter final : public Diag_Reporter { ::Napi::Array diagnostics_; const LSP_Locator* locator_; ::Napi::Value document_uri_; + Translator message_translator_; }; }