From 63a0aeeb5d4e01e1d8727c6ebd773221d28e81db Mon Sep 17 00:00:00 2001 From: John Vilk Date: Thu, 5 Dec 2019 20:43:26 -0800 Subject: [PATCH 1/3] Cleaner retypecheck() interface. --- main/lsp/LSPTypechecker.cc | 29 ++++++++++++++++------------- main/lsp/LSPTypechecker.h | 11 ++++++++--- main/lsp/requests/code_action.cc | 9 +-------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/main/lsp/LSPTypechecker.cc b/main/lsp/LSPTypechecker.cc index bbc6b951e99..098263ec0a7 100644 --- a/main/lsp/LSPTypechecker.cc +++ b/main/lsp/LSPTypechecker.cc @@ -456,21 +456,24 @@ LSPQueryResult LSPTypechecker::query(const core::lsp::Query &q, const std::vecto return LSPQueryResult{move(out.second)}; } -TypecheckRun LSPTypechecker::retypecheck(LSPFileUpdates updates) const { - if (!updates.canTakeFastPath) { - Exception::raise("Tried to typecheck slow path updates with retypecheck. Retypecheck can only typecheck the " - "previously typechecked version of a file."); - } - - for (const auto &file : updates.updatedFiles) { - auto path = file->path(); - auto source = file->source(); - auto fref = gs->findFileByPath(path); - if (!fref.exists() || fref.data(*gs).source() != source) { - Exception::raise("Retypecheck can only typecheck the previously typechecked version of a file."); - } +LSPFileUpdates LSPTypechecker::getNoopUpdate(std::vector frefs) const { + LSPFileUpdates noop; + noop.canTakeFastPath = true; + // Epoch isn't important for this update. + noop.versionStart = 0; + noop.versionEnd = 0; + for (auto fref : frefs) { + auto &index = getIndexed(fref); + noop.updatedFileIndexes.push_back({index.tree->deepCopy(), index.file}); + noop.updatedFiles.push_back(gs->getFiles()[fref.id()]); + noop.updatedFileHashes.push_back(globalStateHashes[fref.id()]); } + return noop; +} +TypecheckRun LSPTypechecker::retypecheck(vector frefs) const { + LSPFileUpdates updates = getNoopUpdate(move(frefs)); + auto workers = WorkerPool::create(0, *config->logger); return runTypechecking(move(updates)); } diff --git a/main/lsp/LSPTypechecker.h b/main/lsp/LSPTypechecker.h index de41574a715..f27c3aaddbd 100644 --- a/main/lsp/LSPTypechecker.h +++ b/main/lsp/LSPTypechecker.h @@ -76,6 +76,12 @@ class LSPTypechecker final { * sending diagnostics to the editor. */ void commitTypecheckRun(TypecheckRun run); + /** + * Get an LSPFileUpdates containing the latest versions of the given files. It's a "no-op" file update because it + * doesn't actually change anything. + */ + LSPFileUpdates getNoopUpdate(std::vector frefs) const; + public: LSPTypechecker(const std::shared_ptr &config); ~LSPTypechecker() = default; @@ -95,10 +101,9 @@ class LSPTypechecker final { bool typecheck(LSPFileUpdates updates); /** - * Re-typechecks the provided input to re-produce error messages. Input *must* match already committed state! - * Provided to facilitate code actions. + * Re-typechecks the provided files to re-produce error messages. */ - TypecheckRun retypecheck(LSPFileUpdates updates) const; + TypecheckRun retypecheck(std::vector frefs) const; /** Runs the provided query against the given files, and returns matches. */ LSPQueryResult query(const core::lsp::Query &q, const std::vector &filesForQuery) const; diff --git a/main/lsp/requests/code_action.cc b/main/lsp/requests/code_action.cc index fee459662bd..4d53e494836 100644 --- a/main/lsp/requests/code_action.cc +++ b/main/lsp/requests/code_action.cc @@ -29,15 +29,8 @@ unique_ptr LSPLoop::handleTextDocumentCodeAction(LSPTypechecker return response; } - LSPFileUpdates updates; - updates.canTakeFastPath = true; - const auto &globalStateHashes = typechecker.getFileHashes(); - ENFORCE(file.id() < globalStateHashes.size()); - updates.updatedFileHashes = {globalStateHashes[file.id()]}; - updates.updatedFiles.push_back(make_shared(string(file.data(gs).path()), string(file.data(gs).source()), - core::File::Type::Normal)); // Simply querying the file in question is insufficient since indexing errors would not be detected. - auto run = typechecker.retypecheck(move(updates)); + auto run = typechecker.retypecheck({file}); auto loc = params.range->toLoc(gs, file); for (auto &error : run.errors) { if (!error->isSilenced && !error->autocorrects.empty()) { From 117f51934cb6ce1c882f54347ad55a2b30658ad3 Mon Sep 17 00:00:00 2001 From: John Vilk Date: Fri, 6 Dec 2019 13:33:02 -0800 Subject: [PATCH 2/3] Remove dead code --- main/lsp/LSPTypechecker.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/main/lsp/LSPTypechecker.cc b/main/lsp/LSPTypechecker.cc index 098263ec0a7..92a3bcaa368 100644 --- a/main/lsp/LSPTypechecker.cc +++ b/main/lsp/LSPTypechecker.cc @@ -473,7 +473,6 @@ LSPFileUpdates LSPTypechecker::getNoopUpdate(std::vector frefs) c TypecheckRun LSPTypechecker::retypecheck(vector frefs) const { LSPFileUpdates updates = getNoopUpdate(move(frefs)); - auto workers = WorkerPool::create(0, *config->logger); return runTypechecking(move(updates)); } From 224438419263555875c88534d1d08be8fed13241 Mon Sep 17 00:00:00 2001 From: John Vilk Date: Fri, 6 Dec 2019 13:33:51 -0800 Subject: [PATCH 3/3] Use indexed directly instead. --- main/lsp/LSPTypechecker.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main/lsp/LSPTypechecker.cc b/main/lsp/LSPTypechecker.cc index 92a3bcaa368..0e1c569484f 100644 --- a/main/lsp/LSPTypechecker.cc +++ b/main/lsp/LSPTypechecker.cc @@ -463,7 +463,9 @@ LSPFileUpdates LSPTypechecker::getNoopUpdate(std::vector frefs) c noop.versionStart = 0; noop.versionEnd = 0; for (auto fref : frefs) { - auto &index = getIndexed(fref); + ENFORCE(fref.exists()); + ENFORCE(fref.id() < indexed.size()); + auto &index = indexed[fref.id()]; noop.updatedFileIndexes.push_back({index.tree->deepCopy(), index.file}); noop.updatedFiles.push_back(gs->getFiles()[fref.id()]); noop.updatedFileHashes.push_back(globalStateHashes[fref.id()]);