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

feat(vscode): make quick-lint a jerk πŸ–• #1190

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions plugin/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
],
"default": "off",
"description": "Log document changes. Useful for quick-lint-js contributors."
},
"quick-lint-js.snarky": {
"scope": "window",
"type": "boolean",
"default": false,
"description": "Add spice to your failures."
}
}
},
Expand Down
66 changes: 52 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,32 @@ 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::on_translator_changed(
::Napi::Env env, QLJS_Workspace& workspace,
VSCode_Diagnostic_Collection diagnostic_collection) {
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 +108,36 @@ 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::on_translator_changed(
::Napi::Env env, QLJS_Workspace& workspace,
VSCode_Diagnostic_Collection diagnostic_collection) {
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
44 changes: 17 additions & 27 deletions plugin/vscode/quick-lint-js/vscode/qljs-document.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class QLJS_Document_Base {
VSCode_Diagnostic_Collection,
Loaded_Config_File* config_file) = 0;

virtual void on_translator_changed(
::Napi::Env env, QLJS_Workspace& workspace,
VSCode_Diagnostic_Collection diagnostic_collection) = 0;

protected:
::Napi::Value uri() { return this->vscode_document_.Value().uri(); }

Expand All @@ -118,21 +122,17 @@ class QLJS_Config_Document : public QLJS_Document_Base {
void on_config_file_changed(::Napi::Env, QLJS_Workspace&,
VSCode_Diagnostic_Collection,
Loaded_Config_File* config_file) override;
void on_translator_changed(
::Napi::Env env, QLJS_Workspace& workspace,
VSCode_Diagnostic_Collection diagnostic_collection) 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 @@ -149,24 +149,14 @@ class QLJS_Lintable_Document : public QLJS_Document_Base {
void on_config_file_changed(::Napi::Env, QLJS_Workspace&,
VSCode_Diagnostic_Collection,
Loaded_Config_File* config_file) override;
void on_translator_changed(::Napi::Env, QLJS_Workspace&,
VSCode_Diagnostic_Collection) 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
30 changes: 29 additions & 1 deletion plugin/vscode/quick-lint-js/vscode/qljs-workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class Extension_Configuration {
}
}

bool get_snarky(::Napi::Env env) {
::Napi::Value value = this->get(env, "snarky");
if (!value.IsBoolean()) {
return false;
}
return value.As<::Napi::Boolean>();
}

::Napi::Value get(::Napi::Env env, const char* section) {
return this->config_ref_.Get("get").As<::Napi::Function>().Call(
this->config_ref_.Value(), {::Napi::String::New(env, section)});
Expand Down Expand Up @@ -124,7 +132,8 @@ QLJS_Workspace::QLJS_Workspace(const Napi::CallbackInfo& info)
::Napi::Persistent(info[2].As<::Napi::Object>())),
ui_(this) {
QLJS_DEBUG_LOG("Workspace %p: created\n", this);
this->update_logging(info.Env());
configuration_changed(info);

this->fs_change_detection_thread_ =
Thread([this]() -> void { this->run_fs_change_detection_thread(); });
}
Expand Down Expand Up @@ -234,10 +243,29 @@ ::Napi::Value QLJS_Workspace::configuration_changed(
::Napi::Env env = info.Env();

this->update_logging(env);
this->on_translator_changed(env);

return env.Undefined();
}

void QLJS_Workspace::on_translator_changed(::Napi::Env env) {
Extension_Configuration config(env, this->vscode_);
bool is_snarky = config.get_snarky(env);

this->is_snarky_enabled_ = is_snarky;
if (is_snarky) {
this->translator_.use_messages_from_locale("en_US@snarky");
} else {
// TODO(#529): Use the locale from the VS Code configuration.
this->translator_.use_messages_from_source_code();
}
this->qljs_documents_.for_each(
[this, is_snarky, env](::Napi::Value value) -> void {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Compiler was warning about is_snarky being an unused capture. I fixed it by using [&] instead.

QLJS_Document_Base* doc = QLJS_Document_Base::unwrap(value);
doc->on_translator_changed(env, *this, this->diagnostic_collection());
});
}

::Napi::Value QLJS_Workspace::editor_visibility_changed(
const Napi::CallbackInfo& info) {
::Napi::Env env = info.Env();
Expand Down
9 changes: 9 additions & 0 deletions plugin/vscode/quick-lint-js/vscode/qljs-workspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class QLJS_Workspace : public ::Napi::ObjectWrap<QLJS_Workspace> {
// Disable logging if logging is enabled.
void disable_logging();

void on_translator_changed(::Napi::Env env);

~QLJS_Workspace();

::Napi::Value dispose(const ::Napi::CallbackInfo& info);
Expand Down Expand Up @@ -248,6 +250,13 @@ class QLJS_Workspace : public ::Napi::ObjectWrap<QLJS_Workspace> {
QLJS_Workspace* workspace_;
};

public:
bool is_snarky_enabled() const;

private:
bool is_snarky_enabled_ = false;
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
Loading