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

refactor(vscode): inject translator and workspace #1189

Merged
merged 1 commit into from
Jan 25, 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
54 changes: 40 additions & 14 deletions plugin/vscode/quick-lint-js/vscode/qljs-document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -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(
Expand All @@ -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();
}
}

Expand Down
35 changes: 8 additions & 27 deletions plugin/vscode/quick-lint-js/vscode/qljs-document.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions plugin/vscode/quick-lint-js/vscode/qljs-workspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ class QLJS_Workspace : public ::Napi::ObjectWrap<QLJS_Workspace> {
QLJS_Workspace* workspace_;
};

private:
Translator translator_;
bool disposed_ = false;
VSCode_Tracer tracer_;
VSCode_Module vscode_;
Expand Down
15 changes: 10 additions & 5 deletions plugin/vscode/quick-lint-js/vscode/vscode-diag-reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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_; }

Expand All @@ -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);
}

Expand All @@ -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_;
};
}

Expand Down
Loading