Applying more review.

This commit is contained in:
Christian Parpart 2022-10-04 16:42:43 +02:00
parent 02e84ea547
commit e924346e1b
10 changed files with 40 additions and 30 deletions

View File

@ -165,14 +165,14 @@ set(sources
lsp/HandlerBase.h lsp/HandlerBase.h
lsp/LanguageServer.cpp lsp/LanguageServer.cpp
lsp/LanguageServer.h lsp/LanguageServer.h
lsp/SemanticHighlight.cpp lsp/SemanticHighlighter.cpp
lsp/SemanticHighlight.h lsp/SemanticHighlighter.h
lsp/SemanticTokensBuilder.cpp lsp/SemanticTokensBuilder.cpp
lsp/SemanticTokensBuilder.h lsp/SemanticTokensBuilder.h
lsp/ReferenceCollector.cpp lsp/ReferenceCollector.cpp
lsp/ReferenceCollector.h lsp/ReferenceCollector.h
lsp/References.cpp lsp/ReferencesHandler.cpp
lsp/References.h lsp/ReferencesHandler.h
lsp/Transport.cpp lsp/Transport.cpp
lsp/Transport.h lsp/Transport.h
lsp/Utils.cpp lsp/Utils.cpp

View File

@ -1,4 +1,4 @@
/* /*LanguageServer.cpp
This file is part of solidity. This file is part of solidity.
solidity is free software: you can redistribute it and/or modify solidity is free software: you can redistribute it and/or modify
@ -26,9 +26,9 @@
// LSP feature implementations // LSP feature implementations
#include <libsolidity/lsp/GotoDefinition.h> #include <libsolidity/lsp/GotoDefinition.h>
#include <libsolidity/lsp/References.h> #include <libsolidity/lsp/ReferencesHandler.h>
#include <libsolidity/lsp/RenameSymbol.h> #include <libsolidity/lsp/RenameSymbol.h>
#include <libsolidity/lsp/SemanticHighlight.h> #include <libsolidity/lsp/SemanticHighlighter.h>
#include <libsolidity/lsp/SemanticTokensBuilder.h> #include <libsolidity/lsp/SemanticTokensBuilder.h>
#include <liblangutil/SourceReferenceExtractor.h> #include <liblangutil/SourceReferenceExtractor.h>
@ -147,10 +147,10 @@ LanguageServer::LanguageServer(Transport& _transport):
{"textDocument/didChange", bind(&LanguageServer::handleTextDocumentDidChange, this, _2)}, {"textDocument/didChange", bind(&LanguageServer::handleTextDocumentDidChange, this, _2)},
{"textDocument/didClose", bind(&LanguageServer::handleTextDocumentDidClose, this, _2)}, {"textDocument/didClose", bind(&LanguageServer::handleTextDocumentDidClose, this, _2)},
{"textDocument/rename", RenameSymbol(*this) }, {"textDocument/rename", RenameSymbol(*this) },
{"textDocument/documentHighlight", SemanticHighlight(*this) }, {"textDocument/documentHighlight", SemanticHighlighter(*this) },
{"textDocument/implementation", GotoDefinition(*this) }, {"textDocument/implementation", GotoDefinition(*this) },
{"textDocument/semanticTokens/full", bind(&LanguageServer::semanticTokensFull, this, _1, _2)}, {"textDocument/semanticTokens/full", bind(&LanguageServer::semanticTokensFull, this, _1, _2)},
{"textDocument/references", References(*this) }, {"textDocument/references", ReferencesHandler(*this) },
{"workspace/didChangeConfiguration", bind(&LanguageServer::handleWorkspaceDidChangeConfiguration, this, _2)}, {"workspace/didChangeConfiguration", bind(&LanguageServer::handleWorkspaceDidChangeConfiguration, this, _2)},
}, },
m_fileRepository("/" /* basePath */, {} /* no search paths */), m_fileRepository("/" /* basePath */, {} /* no search paths */),
@ -187,7 +187,7 @@ void LanguageServer::changeConfiguration(Json::Value const& _settings)
else if (text == "directly-opened-and-on-import") else if (text == "directly-opened-and-on-import")
m_fileLoadStrategy = FileLoadStrategy::DirectlyOpenedAndOnImported; m_fileLoadStrategy = FileLoadStrategy::DirectlyOpenedAndOnImported;
else else
lspRequire(false, ErrorCode::InvalidParams, "Invalid file load strategy: " + text); lspAssert(false, "Invalid file load strategy: " + text);
} }
m_settingsObject = _settings; m_settingsObject = _settings;

View File

@ -107,7 +107,7 @@ bool ReferenceCollector::visit(ImportDirective const& _import)
void ReferenceCollector::endVisit(Identifier const& _identifier) void ReferenceCollector::endVisit(Identifier const& _identifier)
{ {
if (Declaration const* declaration = _identifier.annotation().referencedDeclaration; declaration == &m_declaration) if (_identifier.annotation().referencedDeclaration == &m_declaration)
m_collectedReferences.emplace_back(_identifier.location(), m_kind); m_collectedReferences.emplace_back(_identifier.location(), m_kind);
} }
@ -133,12 +133,18 @@ void ReferenceCollector::endVisit(MemberAccess const& _memberAccess)
bool ReferenceCollector::visit(Assignment const& _assignment) bool ReferenceCollector::visit(Assignment const& _assignment)
{ {
auto const getDocumentHighlightKind = [](Expression const& _expression) noexcept -> DocumentHighlightKind {
if (_expression.annotation().willBeWrittenTo)
return DocumentHighlightKind::Write;
return DocumentHighlightKind::Read;
};
auto const restoreKind = ScopeGuard{[this, savedKind=m_kind]() { m_kind = savedKind; }}; auto const restoreKind = ScopeGuard{[this, savedKind=m_kind]() { m_kind = savedKind; }};
m_kind = DocumentHighlightKind::Write; m_kind = getDocumentHighlightKind(_assignment.leftHandSide());
_assignment.leftHandSide().accept(*this); _assignment.leftHandSide().accept(*this);
m_kind = DocumentHighlightKind::Read; m_kind = getDocumentHighlightKind(_assignment.rightHandSide());
_assignment.rightHandSide().accept(*this); _assignment.rightHandSide().accept(*this);
return false; return false;

View File

@ -30,7 +30,6 @@ namespace solidity::lsp
*/ */
enum class DocumentHighlightKind enum class DocumentHighlightKind
{ {
Unspecified = 0, //!< could be for example a highlight found in a comment
Text = 1, //!< a textual occurrence Text = 1, //!< a textual occurrence
Read = 2, //!< read access to a variable Read = 2, //!< read access to a variable
Write = 3, //!< write access to a variable Write = 3, //!< write access to a variable

View File

@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
#include <libsolidity/lsp/References.h> #include <libsolidity/lsp/ReferencesHandler.h>
#include <libsolidity/lsp/ReferenceCollector.h> #include <libsolidity/lsp/ReferenceCollector.h>
#include <libsolidity/lsp/LanguageServer.h> #include <libsolidity/lsp/LanguageServer.h>
#include <libsolidity/lsp/Utils.h> #include <libsolidity/lsp/Utils.h>
@ -29,7 +29,7 @@ using namespace solidity::frontend;
namespace solidity::lsp namespace solidity::lsp
{ {
void References::operator()(MessageID _id, Json::Value const& _args) void ReferencesHandler::operator()(MessageID _id, Json::Value const& _args)
{ {
auto const [sourceUnitName, lineColumn] = extractSourceUnitNameAndLineColumn(_args); auto const [sourceUnitName, lineColumn] = extractSourceUnitNameAndLineColumn(_args);

View File

@ -23,10 +23,10 @@ namespace solidity::lsp
/** /**
* Implements JSON RPC for `textDocument/references`. * Implements JSON RPC for `textDocument/references`.
*/ */
class References: public HandlerBase class ReferencesHandler: public HandlerBase
{ {
public: public:
explicit References(LanguageServer& _server): HandlerBase(_server) {} using HandlerBase::HandlerBase;
void operator()(MessageID _id, Json::Value const& _args); void operator()(MessageID _id, Json::Value const& _args);
}; };

View File

@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
#include <libsolidity/lsp/SemanticHighlight.h> #include <libsolidity/lsp/SemanticHighlighter.h>
#include <libsolidity/lsp/Utils.h> #include <libsolidity/lsp/Utils.h>
using namespace std; using namespace std;
@ -23,24 +23,23 @@ using namespace solidity;
using namespace solidity::lsp; using namespace solidity::lsp;
using namespace solidity::frontend; using namespace solidity::frontend;
void SemanticHighlight::operator()(MessageID _id, Json::Value const& _args) void SemanticHighlighter::operator()(MessageID _id, Json::Value const& _args)
{ {
auto const [sourceUnitName, lineColumn] = extractSourceUnitNameAndLineColumn(_args); auto const [sourceUnitName, lineColumn] = extractSourceUnitNameAndLineColumn(_args);
auto const [sourceNode, sourceOffset] = m_server.astNodeAndOffsetAtSourceLocation(sourceUnitName, lineColumn); auto const [sourceNode, sourceOffset] = m_server.astNodeAndOffsetAtSourceLocation(sourceUnitName, lineColumn);
Json::Value jsonReply = Json::arrayValue; Json::Value jsonReply = Json::arrayValue;
for (auto const& [location, kind]: semanticHighlight(sourceNode, sourceOffset, sourceUnitName)) for (auto const& [location, kind]: findAllReferences(sourceNode, sourceOffset, sourceUnitName))
{ {
Json::Value item = Json::objectValue; Json::Value item = Json::objectValue;
item["range"] = toRange(location); item["range"] = toRange(location);
if (kind != DocumentHighlightKind::Unspecified)
item["kind"] = static_cast<int>(kind); item["kind"] = static_cast<int>(kind);
jsonReply.append(item); jsonReply.append(item);
} }
client().reply(_id, jsonReply); client().reply(_id, jsonReply);
} }
vector<Reference> SemanticHighlight::semanticHighlight(ASTNode const* _sourceNode, int _sourceOffset, string const& _sourceUnitName) vector<Reference> SemanticHighlighter::findAllReferences(ASTNode const* _sourceNode, int _sourceOffset, string const& _sourceUnitName)
{ {
if (!_sourceNode) if (!_sourceNode)
return {}; return {};

View File

@ -26,15 +26,15 @@ namespace solidity::lsp
/** /**
* Implements JSON RPC for `textDocument/documentHighlight`. * Implements JSON RPC for `textDocument/documentHighlight`.
*/ */
class SemanticHighlight: public HandlerBase class SemanticHighlighter: public HandlerBase
{ {
public: public:
explicit SemanticHighlight(LanguageServer& _server): HandlerBase(_server) {} using HandlerBase::HandlerBase;
void operator()(MessageID _id, Json::Value const& _args); void operator()(MessageID _id, Json::Value const& _args);
private: private:
std::vector<Reference> semanticHighlight(frontend::ASTNode const* _sourceNode, int _sourceOffset, std::string const& _sourceUnitName); std::vector<Reference> findAllReferences(frontend::ASTNode const* _sourceNode, int _sourceOffset, std::string const& _sourceUnitName);
}; };
} }

View File

@ -87,6 +87,11 @@ private:
); \ ); \
} }
/**
* Fatally halts the process if the condition fails.
*/
#define lspAssert(condition, errorMessage) solAssert((condition), (errorMessage))
/** /**
* Transport layer API * Transport layer API
* *

View File

@ -23,14 +23,14 @@ contract MyContract
// ^^^^^ @ColorType3 // ^^^^^ @ColorType3
// ^^^^^ @ColorType4 // ^^^^^ @ColorType4
{ {
Color result = Color(a); Color Color = Color(a);
// ^^^^^ @ColorType5 // ^^^^^ @ColorType5
// ^^^^^ @ColorType6 // ^^^^^ @ColorType6
if (a != lastColor) if (a != lastColor)
// ^^^^^^^^^ @lastCursor2 // ^^^^^^^^^ @lastCursor2
result = Color.Green; Color = Color.Green;
// ^^^^^ @ColorType7 // ^^^^^ @ColorType7
return result; return Color;
} }
function f() public pure returns (uint) function f() public pure returns (uint)
@ -41,6 +41,7 @@ contract MyContract
} }
// ---- // ----
// lib: @diagnostics 2519
// -> textDocument/documentHighlight { // -> textDocument/documentHighlight {
// "position": @EnumDef // "position": @EnumDef
// } // }