diff --git a/Changelog.md b/Changelog.md index 7818c4f15ae2..af7005c1a11b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -14,6 +14,7 @@ Compiler Features: * SMTChecker: Support ``abi.encodeCall`` taking into account the called selector. * Language Server: Allow full filesystem access to language server. * Language Server: Always adds ``{project_root}/node_modules`` to include search paths. + * Language Server: Adds support for configuring extra include paths ``include-paths`` JSON settings object that can be passed during LSP configuration stage. Bugfixes: diff --git a/libsolidity/lsp/FileRepository.cpp b/libsolidity/lsp/FileRepository.cpp index 05dea971d1cc..bfdca32ac807 100644 --- a/libsolidity/lsp/FileRepository.cpp +++ b/libsolidity/lsp/FileRepository.cpp @@ -38,7 +38,19 @@ using solidity::util::Result; FileRepository::FileRepository(boost::filesystem::path _basePath): m_basePath(std::move(_basePath)) { - m_includePaths.emplace_back(m_basePath / "node_modules"); +} + +void FileRepository::setIncludePaths(std::vector _paths) +{ + // Prefix relative paths with base-path. + for (auto& path: _paths) + if (path.is_relative()) + path = m_basePath / path; + + // Always have {projectRoot}/node_modules in the include-paths. + _paths.emplace_back(m_basePath / "node_modules"); + + m_includePaths = std::move(_paths); } string FileRepository::sourceUnitNameToUri(string const& _sourceUnitName) const diff --git a/libsolidity/lsp/FileRepository.h b/libsolidity/lsp/FileRepository.h index 137fff7e330d..290ec91435e5 100644 --- a/libsolidity/lsp/FileRepository.h +++ b/libsolidity/lsp/FileRepository.h @@ -31,6 +31,8 @@ class FileRepository public: explicit FileRepository(boost::filesystem::path _basePath); + void setIncludePaths(std::vector _paths); + boost::filesystem::path const& basePath() const { return m_basePath; } /// Translates a compiler-internal source unit name to an LSP client path. diff --git a/libsolidity/lsp/LanguageServer.cpp b/libsolidity/lsp/LanguageServer.cpp index 44b0cf041bcd..6bdf1218653f 100644 --- a/libsolidity/lsp/LanguageServer.cpp +++ b/libsolidity/lsp/LanguageServer.cpp @@ -101,6 +101,21 @@ Json::Value LanguageServer::toJson(SourceLocation const& _location) void LanguageServer::changeConfiguration(Json::Value const& _settings) { m_settingsObject = _settings; + if (_settings["include-paths"] && _settings["include-paths"].isArray()) + { + std::vector includePaths; + Json::Value jsonIncludePaths = _settings["includes"]; + for (Json::Value jsonPath : jsonIncludePaths) + { + if (!jsonPath.isString()) + { + m_client.trace("Invalid JSON configuration passed. \"include-paths\" must be an array of strings."); + continue; + } + includePaths.emplace_back(boost::filesystem::path(jsonPath.asString())); + } + m_fileRepository.setIncludePaths(move(includePaths)); + } } void LanguageServer::compile()