|
| 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 | +} |
0 commit comments