Skip to content

Commit 3193dfb

Browse files
authored
Merge pull request #11350 from ethereum/lsp
Language Server
2 parents 1594518 + 927b24d commit 3193dfb

22 files changed

+1891
-3
lines changed

.circleci/config.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ defaults:
198198
- store_artifacts: *artifacts_test_results
199199
- gitter_notify_failure_unless_pr
200200

201+
- steps_test_lsp: &steps_test_lsp
202+
steps:
203+
- checkout
204+
- attach_workspace:
205+
at: build
206+
- run:
207+
name: Install dependencies
208+
command: pip install --user deepdiff colorama
209+
- run:
210+
name: Executing solc LSP test suite
211+
command: ./test/lsp.py ./build/solc/solc
212+
- gitter_notify_failure_unless_pr
213+
201214
- steps_soltest_all: &steps_soltest_all
202215
steps:
203216
- checkout
@@ -519,7 +532,7 @@ jobs:
519532
command: apt -q update && apt install -y python3-pip
520533
- run:
521534
name: Install pylint
522-
command: python3 -m pip install pylint z3-solver pygments-lexer-solidity parsec tabulate
535+
command: python3 -m pip install pylint z3-solver pygments-lexer-solidity parsec tabulate deepdiff colorama
523536
# also z3-solver, parsec and tabulate to make sure pylint knows about this module, pygments-lexer-solidity for docs
524537
- run:
525538
name: Linting Python Scripts
@@ -887,6 +900,10 @@ jobs:
887900
parallelism: 15 # 7 EVM versions, each with/without optimization + 1 ABIv1/@nooptions run
888901
<<: *steps_soltest_all
889902

903+
t_ubu_lsp: &t_ubu_lsp
904+
<<: *base_ubuntu2004_small
905+
<<: *steps_test_lsp
906+
890907
t_archlinux_soltest: &t_archlinux_soltest
891908
<<: *base_archlinux
892909
environment:
@@ -1288,6 +1305,7 @@ workflows:
12881305
- t_ubu_soltest_enforce_yul: *workflow_ubuntu2004
12891306
- b_ubu_clang: *workflow_trigger_on_tags
12901307
- t_ubu_clang_soltest: *workflow_ubuntu2004_clang
1308+
- t_ubu_lsp: *workflow_ubuntu2004
12911309

12921310
# Ubuntu fake release build and tests
12931311
- b_ubu_release: *workflow_trigger_on_tags

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Language Features:
55

66

77
Compiler Features:
8+
* Commandline Interface: Add ``--lsp`` option to get ``solc`` to act as a Language Server (LSP) communicating over stdio.
89

910

1011
Bugfixes:

libsolidity/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ set(sources
155155
interface/StorageLayout.h
156156
interface/Version.cpp
157157
interface/Version.h
158+
lsp/LanguageServer.cpp
159+
lsp/LanguageServer.h
160+
lsp/FileRepository.cpp
161+
lsp/FileRepository.h
162+
lsp/Transport.cpp
163+
lsp/Transport.h
158164
parsing/DocStringParser.cpp
159165
parsing/DocStringParser.h
160166
parsing/Parser.cpp

libsolidity/lsp/FileRepository.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
19+
#include <libsolidity/lsp/FileRepository.h>
20+
21+
using namespace std;
22+
using namespace solidity;
23+
using namespace solidity::lsp;
24+
25+
namespace
26+
{
27+
28+
string stripFilePrefix(string const& _path)
29+
{
30+
if (_path.find("file://") == 0)
31+
return _path.substr(7);
32+
else
33+
return _path;
34+
}
35+
36+
}
37+
38+
string FileRepository::sourceUnitNameToClientPath(string const& _sourceUnitName) const
39+
{
40+
if (m_sourceUnitNamesToClientPaths.count(_sourceUnitName))
41+
return m_sourceUnitNamesToClientPaths.at(_sourceUnitName);
42+
else if (_sourceUnitName.find("file://") == 0)
43+
return _sourceUnitName;
44+
else
45+
return "file://" + (m_fileReader.basePath() / _sourceUnitName).generic_string();
46+
}
47+
48+
string FileRepository::clientPathToSourceUnitName(string const& _path) const
49+
{
50+
return m_fileReader.cliPathToSourceUnitName(stripFilePrefix(_path));
51+
}
52+
53+
map<string, string> const& FileRepository::sourceUnits() const
54+
{
55+
return m_fileReader.sourceUnits();
56+
}
57+
58+
void FileRepository::setSourceByClientPath(string const& _uri, string _text)
59+
{
60+
// This is needed for uris outside the base path. It can lead to collisions,
61+
// but we need to mostly rewrite this in a future version anyway.
62+
m_sourceUnitNamesToClientPaths.emplace(clientPathToSourceUnitName(_uri), _uri);
63+
m_fileReader.addOrUpdateFile(stripFilePrefix(_uri), move(_text));
64+
}

libsolidity/lsp/FileRepository.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
#pragma once
19+
20+
#include <libsolidity/interface/FileReader.h>
21+
22+
#include <string>
23+
#include <map>
24+
25+
namespace solidity::lsp
26+
{
27+
28+
class FileRepository
29+
{
30+
public:
31+
explicit FileRepository(boost::filesystem::path const& _basePath):
32+
m_fileReader(_basePath) {}
33+
34+
boost::filesystem::path const& basePath() const { return m_fileReader.basePath(); }
35+
36+
/// Translates a compiler-internal source unit name to an LSP client path.
37+
std::string sourceUnitNameToClientPath(std::string const& _sourceUnitName) const;
38+
/// Translates an LSP client path into a compiler-internal source unit name.
39+
std::string clientPathToSourceUnitName(std::string const& _uri) const;
40+
41+
/// @returns all sources by their compiler-internal source unit name.
42+
std::map<std::string, std::string> const& sourceUnits() const;
43+
/// Changes the source identified by the LSP client path _uri to _text.
44+
void setSourceByClientPath(std::string const& _uri, std::string _text);
45+
46+
frontend::ReadCallback::Callback reader() { return m_fileReader.reader(); }
47+
48+
private:
49+
std::map<std::string, std::string> m_sourceUnitNamesToClientPaths;
50+
frontend::FileReader m_fileReader;
51+
};
52+
53+
}

0 commit comments

Comments
 (0)