Skip to content

Commit

Permalink
Language Server: Adds support for configuring extra include paths ``i…
Browse files Browse the repository at this point in the history
…nclude-paths`` JSON settings object that can be passed during LSP configuration stage.
  • Loading branch information
christianparpart committed May 16, 2022
1 parent f6c6179 commit 91283c4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 16 additions & 2 deletions libsolidity/lsp/FileRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,23 @@ using solidity::util::readFileAsString;
using solidity::util::joinHumanReadable;
using solidity::util::Result;

FileRepository::FileRepository(boost::filesystem::path _basePath): m_basePath(std::move(_basePath))
FileRepository::FileRepository(boost::filesystem::path _basePath, std::vector<boost::filesystem::path> _paths):
m_basePath(std::move(_basePath))
{
m_includePaths.emplace_back(m_basePath / "node_modules");
setIncludePaths(move(_paths));
}

void FileRepository::setIncludePaths(std::vector<boost::filesystem::path> _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
Expand Down
5 changes: 4 additions & 1 deletion libsolidity/lsp/FileRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ namespace solidity::lsp
class FileRepository
{
public:
explicit FileRepository(boost::filesystem::path _basePath);
FileRepository(boost::filesystem::path _basePath, std::vector<boost::filesystem::path> _paths);

std::vector<boost::filesystem::path> const& includePaths() const noexcept { return m_includePaths; }
void setIncludePaths(std::vector<boost::filesystem::path> _paths);

boost::filesystem::path const& basePath() const { return m_basePath; }

Expand Down
21 changes: 18 additions & 3 deletions libsolidity/lsp/LanguageServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ LanguageServer::LanguageServer(Transport& _transport):
{"textDocument/implementation", GotoDefinition(*this) },
{"workspace/didChangeConfiguration", bind(&LanguageServer::handleWorkspaceDidChangeConfiguration, this, _2)},
},
m_fileRepository("/" /* basePath */),
m_fileRepository("/" /* basePath */, {} /* no search paths */),
m_compilerStack{m_fileRepository.reader()}
{
}
Expand All @@ -101,14 +101,29 @@ Json::Value LanguageServer::toJson(SourceLocation const& _location)
void LanguageServer::changeConfiguration(Json::Value const& _settings)
{
m_settingsObject = _settings;
Json::Value jsonIncludePaths = _settings["include-paths"];
if (jsonIncludePaths && jsonIncludePaths.isArray())
{
std::vector<boost::filesystem::path> includePaths;
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()
{
// For files that are not open, we have to take changes on disk into account,
// 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);

for (string const& fileName: m_openFiles)
Expand Down Expand Up @@ -255,7 +270,7 @@ void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
else if (Json::Value rootPath = _args["rootPath"])
rootPath = rootPath.asString();

m_fileRepository = FileRepository(rootPath);
m_fileRepository = FileRepository(rootPath, {});
if (_args["initializationOptions"].isObject())
changeConfiguration(_args["initializationOptions"]);

Expand Down

0 comments on commit 91283c4

Please sign in to comment.