2021-12-20 13:10:17 +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
|
|
|
|
|
2022-01-05 11:15:10 +00:00
|
|
|
#include <liblangutil/CharStreamProvider.h>
|
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/lsp/FileRepository.h>
|
|
|
|
#include <libsolidity/lsp/Utils.h>
|
|
|
|
|
2022-04-25 13:35:41 +00:00
|
|
|
#include <regex>
|
2021-12-20 13:10:17 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2022-01-05 11:15:10 +00:00
|
|
|
namespace solidity::lsp
|
|
|
|
{
|
|
|
|
|
|
|
|
using namespace frontend;
|
|
|
|
using namespace langutil;
|
|
|
|
|
2023-08-15 14:21:33 +00:00
|
|
|
std::optional<LineColumn> parseLineColumn(Json::Value const& _lineColumn)
|
2022-01-05 11:15:10 +00:00
|
|
|
{
|
|
|
|
if (_lineColumn.isObject() && _lineColumn["line"].isInt() && _lineColumn["character"].isInt())
|
|
|
|
return LineColumn{_lineColumn["line"].asInt(), _lineColumn["character"].asInt()};
|
|
|
|
else
|
2023-08-15 14:21:33 +00:00
|
|
|
return std::nullopt;
|
2022-01-05 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 13:10:17 +00:00
|
|
|
Json::Value toJson(LineColumn const& _pos)
|
2022-01-05 11:15:10 +00:00
|
|
|
{
|
|
|
|
Json::Value json = Json::objectValue;
|
2023-08-15 14:21:33 +00:00
|
|
|
json["line"] = std::max(_pos.line, 0);
|
|
|
|
json["character"] = std::max(_pos.column, 0);
|
2022-01-05 11:15:10 +00:00
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value toJsonRange(LineColumn const& _start, LineColumn const& _end)
|
|
|
|
{
|
|
|
|
Json::Value json;
|
|
|
|
json["start"] = toJson(_start);
|
|
|
|
json["end"] = toJson(_end);
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2021-12-20 13:10:17 +00:00
|
|
|
Declaration const* referencedDeclaration(Expression const* _expression)
|
2022-01-05 11:15:10 +00:00
|
|
|
{
|
|
|
|
if (auto const* identifier = dynamic_cast<Identifier const*>(_expression))
|
2021-12-20 13:10:17 +00:00
|
|
|
if (Declaration const* referencedDeclaration = identifier->annotation().referencedDeclaration)
|
|
|
|
return referencedDeclaration;
|
|
|
|
|
|
|
|
if (auto const* memberAccess = dynamic_cast<MemberAccess const*>(_expression))
|
|
|
|
if (memberAccess->annotation().referencedDeclaration)
|
|
|
|
return memberAccess->annotation().referencedDeclaration;
|
|
|
|
|
|
|
|
return nullptr;
|
2022-01-05 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 14:21:33 +00:00
|
|
|
std::optional<SourceLocation> declarationLocation(Declaration const* _declaration)
|
2022-01-05 11:15:10 +00:00
|
|
|
{
|
|
|
|
if (!_declaration)
|
2023-08-15 14:21:33 +00:00
|
|
|
return std::nullopt;
|
2022-01-05 11:15:10 +00:00
|
|
|
|
|
|
|
if (_declaration->nameLocation().isValid())
|
|
|
|
return _declaration->nameLocation();
|
|
|
|
|
|
|
|
if (_declaration->location().isValid())
|
|
|
|
return _declaration->location();
|
|
|
|
|
2023-08-15 14:21:33 +00:00
|
|
|
return std::nullopt;
|
2022-01-05 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 14:21:33 +00:00
|
|
|
std::optional<SourceLocation> parsePosition(
|
2021-12-20 13:10:17 +00:00
|
|
|
FileRepository const& _fileRepository,
|
2023-08-15 14:21:33 +00:00
|
|
|
std::string const& _sourceUnitName,
|
2021-12-20 13:10:17 +00:00
|
|
|
Json::Value const& _position
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!_fileRepository.sourceUnits().count(_sourceUnitName))
|
2023-08-15 14:21:33 +00:00
|
|
|
return std::nullopt;
|
2021-12-20 13:10:17 +00:00
|
|
|
|
2023-08-15 14:21:33 +00:00
|
|
|
if (std::optional<LineColumn> lineColumn = parseLineColumn(_position))
|
|
|
|
if (std::optional<int> const offset = CharStream::translateLineColumnToPosition(
|
2021-12-20 13:10:17 +00:00
|
|
|
_fileRepository.sourceUnits().at(_sourceUnitName),
|
|
|
|
*lineColumn
|
|
|
|
))
|
2023-08-15 14:21:33 +00:00
|
|
|
return SourceLocation{*offset, *offset, std::make_shared<std::string>(_sourceUnitName)};
|
|
|
|
return std::nullopt;
|
2021-12-20 13:10:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 14:21:33 +00:00
|
|
|
std::optional<SourceLocation> parseRange(FileRepository const& _fileRepository, std::string const& _sourceUnitName, Json::Value const& _range)
|
2021-12-20 13:10:17 +00:00
|
|
|
{
|
|
|
|
if (!_range.isObject())
|
2023-08-15 14:21:33 +00:00
|
|
|
return std::nullopt;
|
|
|
|
std::optional<SourceLocation> start = parsePosition(_fileRepository, _sourceUnitName, _range["start"]);
|
|
|
|
std::optional<SourceLocation> end = parsePosition(_fileRepository, _sourceUnitName, _range["end"]);
|
2021-12-20 13:10:17 +00:00
|
|
|
if (!start || !end)
|
2023-08-15 14:21:33 +00:00
|
|
|
return std::nullopt;
|
2021-12-20 13:10:17 +00:00
|
|
|
solAssert(*start->sourceName == *end->sourceName);
|
|
|
|
start->end = end->end;
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2023-08-15 14:21:33 +00:00
|
|
|
std::string stripFileUriSchemePrefix(std::string const& _path)
|
2022-04-25 13:35:41 +00:00
|
|
|
{
|
2023-08-15 14:21:33 +00:00
|
|
|
std::regex const windowsDriveLetterPath("^file:///[a-zA-Z]:/");
|
2022-04-25 13:35:41 +00:00
|
|
|
if (regex_search(_path, windowsDriveLetterPath))
|
|
|
|
return _path.substr(8);
|
|
|
|
if (_path.find("file://") == 0)
|
|
|
|
return _path.substr(7);
|
|
|
|
else
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
2022-01-05 11:15:10 +00:00
|
|
|
}
|