2021-12-13 13:53:40 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsolidity/lsp/Transport.h>
|
|
|
|
#include <libsolidity/lsp/FileRepository.h>
|
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
|
|
|
#include <libsolidity/interface/FileReader.h>
|
|
|
|
|
|
|
|
#include <json/value.h>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace solidity::lsp
|
|
|
|
{
|
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
class RenameSymbol;
|
2021-12-13 13:53:40 +00:00
|
|
|
enum class ErrorCode;
|
|
|
|
|
2022-06-13 13:56:55 +00:00
|
|
|
/**
|
|
|
|
* Enum to mandate what files to take into consideration for source code analysis.
|
|
|
|
*/
|
|
|
|
enum class FileLoadStrategy
|
|
|
|
{
|
|
|
|
/// Takes only those files into consideration that are explicitly opened and those
|
|
|
|
/// that have been directly or indirectly imported.
|
|
|
|
DirectlyOpenedAndOnImported = 0,
|
|
|
|
|
|
|
|
/// Takes all Solidity (.sol) files within the project root into account.
|
|
|
|
/// Symbolic links will be followed, even if they lead outside of the project directory
|
|
|
|
/// (`--allowed-paths` is currently ignored by the LSP).
|
|
|
|
///
|
|
|
|
/// This resembles the closest what other LSPs should be doing already.
|
|
|
|
ProjectDirectory = 1,
|
|
|
|
};
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
/**
|
|
|
|
* Solidity Language Server, managing one LSP client.
|
|
|
|
* This implements a subset of LSP version 3.16 that can be found at:
|
|
|
|
* https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/
|
|
|
|
*/
|
|
|
|
class LanguageServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// @param _transport Customizable transport layer.
|
|
|
|
explicit LanguageServer(Transport& _transport);
|
|
|
|
|
|
|
|
/// Re-compiles the project and updates the diagnostics pushed to the client.
|
|
|
|
void compileAndUpdateDiagnostics();
|
|
|
|
|
|
|
|
/// Loops over incoming messages via the transport layer until shutdown condition is met.
|
|
|
|
///
|
|
|
|
/// The standard shutdown condition is when the maximum number of consecutive failures
|
|
|
|
/// has been exceeded.
|
|
|
|
///
|
|
|
|
/// @return boolean indicating normal or abnormal termination.
|
|
|
|
bool run();
|
|
|
|
|
2022-01-05 11:15:10 +00:00
|
|
|
FileRepository& fileRepository() noexcept { return m_fileRepository; }
|
|
|
|
Transport& client() noexcept { return m_client; }
|
2022-10-04 15:54:18 +00:00
|
|
|
std::tuple<frontend::ASTNode const*, int> astNodeAndOffsetAtSourceLocation(std::string const& _sourceUnitName, langutil::LineColumn const& _filePos);
|
2021-12-20 13:10:17 +00:00
|
|
|
frontend::ASTNode const* astNodeAtSourceLocation(std::string const& _sourceUnitName, langutil::LineColumn const& _filePos);
|
2022-05-23 12:20:03 +00:00
|
|
|
frontend::CompilerStack const& compilerStack() const noexcept { return m_compilerStack; }
|
2022-01-05 11:15:10 +00:00
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
private:
|
|
|
|
/// Checks if the server is initialized (to be used by messages that need it to be initialized).
|
|
|
|
/// Reports an error and returns false if not.
|
2022-01-03 15:19:04 +00:00
|
|
|
void requireServerInitialized();
|
2021-12-13 13:53:40 +00:00
|
|
|
void handleInitialize(MessageID _id, Json::Value const& _args);
|
2022-06-13 13:56:55 +00:00
|
|
|
void handleInitialized(MessageID _id, Json::Value const& _args);
|
2022-01-03 15:19:04 +00:00
|
|
|
void handleWorkspaceDidChangeConfiguration(Json::Value const& _args);
|
2022-04-05 11:38:59 +00:00
|
|
|
void setTrace(Json::Value const& _args);
|
2022-01-03 15:19:04 +00:00
|
|
|
void handleTextDocumentDidOpen(Json::Value const& _args);
|
|
|
|
void handleTextDocumentDidChange(Json::Value const& _args);
|
|
|
|
void handleTextDocumentDidClose(Json::Value const& _args);
|
2022-05-23 12:20:03 +00:00
|
|
|
void handleRename(Json::Value const& _args);
|
2021-12-20 13:10:17 +00:00
|
|
|
void handleGotoDefinition(MessageID _id, Json::Value const& _args);
|
2022-05-03 14:32:48 +00:00
|
|
|
void semanticTokensFull(MessageID _id, Json::Value const& _args);
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
/// Invoked when the server user-supplied configuration changes (initiated by the client).
|
|
|
|
void changeConfiguration(Json::Value const&);
|
|
|
|
|
|
|
|
/// Compile everything until after analysis phase.
|
|
|
|
void compile();
|
2022-06-13 13:56:55 +00:00
|
|
|
|
|
|
|
std::vector<boost::filesystem::path> allSolidityFilesFromProject() const;
|
|
|
|
|
2022-01-05 11:15:10 +00:00
|
|
|
using MessageHandler = std::function<void(MessageID, Json::Value const&)>;
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-01-05 11:15:10 +00:00
|
|
|
Json::Value toRange(langutil::SourceLocation const& _location);
|
|
|
|
Json::Value toJson(langutil::SourceLocation const& _location);
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
// LSP related member fields
|
|
|
|
|
|
|
|
enum class State { Started, Initialized, ShutdownRequested, ExitRequested, ExitWithoutShutdown };
|
|
|
|
State m_state = State::Started;
|
|
|
|
|
|
|
|
Transport& m_client;
|
|
|
|
std::map<std::string, MessageHandler> m_handlers;
|
|
|
|
|
2022-04-25 13:35:41 +00:00
|
|
|
/// Set of files (names in URI form) known to be open by the client.
|
2021-12-13 13:53:40 +00:00
|
|
|
std::set<std::string> m_openFiles;
|
|
|
|
/// Set of source unit names for which we sent diagnostics to the client in the last iteration.
|
|
|
|
std::set<std::string> m_nonemptyDiagnostics;
|
|
|
|
FileRepository m_fileRepository;
|
2022-06-13 13:56:55 +00:00
|
|
|
FileLoadStrategy m_fileLoadStrategy = FileLoadStrategy::ProjectDirectory;
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
frontend::CompilerStack m_compilerStack;
|
|
|
|
|
|
|
|
/// User-supplied custom configuration settings (such as EVM version).
|
|
|
|
Json::Value m_settingsObject;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|