Allow to set include paths in LSP.

This commit is contained in:
chriseth 2021-12-30 13:34:20 +01:00
parent 692614df72
commit bc8d686868
3 changed files with 25 additions and 6 deletions

View File

@ -5,7 +5,7 @@ Language Features:
Compiler Features: Compiler Features:
* Language Server: Option to set include paths.
Bugfixes: Bugfixes:

View File

@ -28,10 +28,15 @@ namespace solidity::lsp
class FileRepository class FileRepository
{ {
public: public:
explicit FileRepository(boost::filesystem::path const& _basePath): FileRepository(
m_fileReader(_basePath) {} boost::filesystem::path const& _basePath,
std::vector<boost::filesystem::path> const& _includePaths
):
m_fileReader(_basePath, _includePaths)
{}
boost::filesystem::path const& basePath() const { return m_fileReader.basePath(); } boost::filesystem::path const& basePath() const { return m_fileReader.basePath(); }
std::vector<boost::filesystem::path> const& includePaths() const { return m_fileReader.includePaths(); }
/// Translates a compiler-internal source unit name to an LSP client path. /// Translates a compiler-internal source unit name to an LSP client path.
std::string sourceUnitNameToClientPath(std::string const& _sourceUnitName) const; std::string sourceUnitNameToClientPath(std::string const& _sourceUnitName) const;

View File

@ -101,7 +101,7 @@ LanguageServer::LanguageServer(Transport& _transport):
{"textDocument/didClose", bind(&LanguageServer::handleTextDocumentDidClose, this, _1, _2)}, {"textDocument/didClose", bind(&LanguageServer::handleTextDocumentDidClose, this, _1, _2)},
{"workspace/didChangeConfiguration", bind(&LanguageServer::handleWorkspaceDidChangeConfiguration, this, _1, _2)}, {"workspace/didChangeConfiguration", bind(&LanguageServer::handleWorkspaceDidChangeConfiguration, this, _1, _2)},
}, },
m_fileRepository("/" /* basePath */), m_fileRepository("/" /* basePath */, {} /* includePaths */),
m_compilerStack{m_fileRepository.reader()} m_compilerStack{m_fileRepository.reader()}
{ {
} }
@ -160,6 +160,20 @@ Json::Value LanguageServer::toJson(SourceLocation const& _location) const
void LanguageServer::changeConfiguration(Json::Value const& _settings) void LanguageServer::changeConfiguration(Json::Value const& _settings)
{ {
m_settingsObject = _settings; m_settingsObject = _settings;
// TODO EVM version
// TODO in solcjs we probably have the include paths handled by the plugin
// - should we still allow this option?
// TODO how do you change the base path - do you restart the server?
std::vector<boost::filesystem::path> includePaths;
if (m_settingsObject.isMember("includePaths"))
for (auto const& path: m_settingsObject["includePaths"])
// TODO these might have `file://` prefixes we have to remove.
includePaths.emplace_back(path.asString());
m_fileRepository = FileRepository(m_fileRepository.basePath(), includePaths);
} }
void LanguageServer::compile() void LanguageServer::compile()
@ -167,7 +181,7 @@ void LanguageServer::compile()
// For files that are not open, we have to take changes on disk into account, // For files that are not open, we have to take changes on disk into account,
// so we just remove all non-open files. // so we just remove all non-open files.
FileRepository oldRepository(m_fileRepository.basePath()); FileRepository oldRepository(m_fileRepository.basePath(), m_fileRepository.includePaths());
swap(oldRepository, m_fileRepository); swap(oldRepository, m_fileRepository);
for (string const& fileName: m_openFiles) for (string const& fileName: m_openFiles)
@ -304,7 +318,7 @@ void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
else if (Json::Value rootPath = _args["rootPath"]) else if (Json::Value rootPath = _args["rootPath"])
rootPath = rootPath.asString(); rootPath = rootPath.asString();
m_fileRepository = FileRepository(boost::filesystem::path(rootPath)); m_fileRepository = FileRepository(boost::filesystem::path(rootPath), {});
if (_args["initializationOptions"].isObject()) if (_args["initializationOptions"].isObject())
changeConfiguration(_args["initializationOptions"]); changeConfiguration(_args["initializationOptions"]);