Compare commits
65 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9630b96935 | ||
|
|
76e34ae628 | ||
|
|
cbb6bdd1ae | ||
|
|
a5af5b505f | ||
|
|
4d120a6a98 | ||
|
|
6e839f7675 | ||
|
|
cade1e2c45 | ||
|
|
d16d3b24ef | ||
|
|
4be7657edb | ||
|
|
040f0b9f0c | ||
|
|
ef2c31c6a3 | ||
|
|
87879ee5df | ||
|
|
01b1fac73e | ||
|
|
5550cb259c | ||
|
|
466c5b7bcc | ||
|
|
0ec120f391 | ||
|
|
5d916b6989 | ||
|
|
1c606ee8a8 | ||
|
|
cdb62adce3 | ||
|
|
6ac3b1d17f | ||
|
|
ce2c3a21db | ||
|
|
3697fae2d3 | ||
|
|
48d5980a36 | ||
|
|
e07b3242c8 | ||
|
|
2d36887b26 | ||
|
|
67aced6544 | ||
|
|
3c171b9e25 | ||
|
|
c8c284e061 | ||
|
|
6bf08e0f14 | ||
|
|
40d93525d9 | ||
|
|
f8136d6dbc | ||
|
|
5af6555d3c | ||
|
|
310c449250 | ||
|
|
bd4f357e59 | ||
|
|
02ae7c4a7c | ||
|
|
6e03b145ba | ||
|
|
b3123db464 | ||
|
|
7d85c2c8dd | ||
|
|
b08dc91e53 | ||
|
|
894b38d55d | ||
|
|
cc9877adbe | ||
|
|
66af586b2a | ||
|
|
c870e3d512 | ||
|
|
9e6fc1a277 | ||
|
|
79864d24ee | ||
|
|
6f93218462 | ||
|
|
600659fe81 | ||
|
|
af59e9b3f6 | ||
|
|
39aa03cd55 | ||
|
|
8d5c6b588c | ||
|
|
b2d8e66ef1 | ||
|
|
4624bdf4e8 | ||
|
|
6628dfe5d0 | ||
|
|
e73b0bab0f | ||
|
|
06f7a4669d | ||
|
|
1a8267bb74 | ||
|
|
9670ecfb59 | ||
|
|
3b8c225d06 | ||
|
|
ff727b7793 | ||
|
|
1ac3bf421d | ||
|
|
7919cf5025 | ||
|
|
b4dd6c840d | ||
|
|
0021c07b56 | ||
|
|
5e2736a710 | ||
|
|
f89f0d6b56 |
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
insert_final_newline = true
|
||||||
|
end_of_line = lf
|
||||||
|
|
||||||
|
[*.{yml,yaml}]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
poetry.lock linguist-generated=true
|
||||||
37
.github/workflows/tests.yml
vendored
37
.github/workflows/tests.yml
vendored
@@ -1,35 +1,42 @@
|
|||||||
name: Tests
|
name: Tests
|
||||||
|
|
||||||
on: [push]
|
on: [pull_request]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
env:
|
|
||||||
CMAKE_VERSION: 3.14.7
|
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-18.04]
|
python: [3.6, 3.7, 3.8, 3.9]
|
||||||
python: [3.6, 3.7, 3.8]
|
os: [ubuntu-18.04, windows-2016]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v2
|
||||||
- name: Cache .tox
|
|
||||||
uses: actions/cache@v1
|
|
||||||
with:
|
|
||||||
path: .tox
|
|
||||||
key: ${{ runner.OS }}-tox-${{ hashFiles('poetry.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.OS }}-tox-
|
|
||||||
- name: Set up Python ${{ matrix.python }}
|
- name: Set up Python ${{ matrix.python }}
|
||||||
uses: actions/setup-python@v1
|
uses: actions/setup-python@v1
|
||||||
with:
|
with:
|
||||||
python-version: ${{ matrix.python }}
|
python-version: ${{ matrix.python }}
|
||||||
|
- name: Setup VC
|
||||||
|
uses: ilammy/msvc-dev-cmd@v1
|
||||||
|
if: contains(matrix.os, 'windows')
|
||||||
|
- name: Install CMake
|
||||||
|
if: contains(matrix.os, 'ubuntu')
|
||||||
|
run: |
|
||||||
|
CMAKE_VERSION=3.17.3
|
||||||
|
curl -sSL https://github.com/Kitware/CMake/releases/download/v$CMAKE_VERSION/cmake-$CMAKE_VERSION-Linux-x86_64.tar.gz | tar xz
|
||||||
|
sudo cp -rT cmake-$CMAKE_VERSION-Linux-x86_64 /usr/local
|
||||||
|
rm -rf cmake-$CMAKE_VERSION-Linux-x86_64
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
curl -sSL https://github.com/Kitware/CMake/releases/download/v$CMAKE_VERSION/cmake-$CMAKE_VERSION-Linux-x86_64.tar.gz | tar xz
|
cmake --version
|
||||||
python -m pip install --upgrade setuptools pip wheel
|
python -m pip install --upgrade setuptools pip wheel
|
||||||
python -m pip install poetry tox-gh-actions
|
python -m pip install poetry tox-gh-actions
|
||||||
- name: Test with tox
|
- name: Test with tox
|
||||||
run: |
|
run: |
|
||||||
export PATH=$GITHUB_WORKSPACE/cmake-$CMAKE_VERSION-Linux-x86_64/bin:$PATH
|
|
||||||
tox
|
tox
|
||||||
|
- name: Upload coverage reports to Codecov
|
||||||
|
uses: codecov/codecov-action@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
flags: unittests
|
||||||
|
name: Python ${{ matrix.python }} on ${{ matrix.os }}
|
||||||
|
fail_ci_if_error: false
|
||||||
|
|||||||
129
.gitignore
vendored
129
.gitignore
vendored
@@ -1,133 +1,4 @@
|
|||||||
### https://raw.github.com/github/gitignore/cb0c6ef7ac68f2300409ee85501d9ad432cb4c7e/Python.gitignore
|
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
|
||||||
*$py.class
|
|
||||||
|
|
||||||
# C extensions
|
|
||||||
*.so
|
|
||||||
|
|
||||||
# Distribution / packaging
|
|
||||||
.Python
|
|
||||||
build/
|
|
||||||
develop-eggs/
|
|
||||||
dist/
|
|
||||||
downloads/
|
|
||||||
eggs/
|
|
||||||
.eggs/
|
|
||||||
lib/
|
|
||||||
lib64/
|
|
||||||
parts/
|
|
||||||
sdist/
|
|
||||||
var/
|
|
||||||
wheels/
|
|
||||||
pip-wheel-metadata/
|
|
||||||
share/python-wheels/
|
|
||||||
*.egg-info/
|
|
||||||
.installed.cfg
|
|
||||||
*.egg
|
|
||||||
MANIFEST
|
|
||||||
|
|
||||||
# PyInstaller
|
|
||||||
# Usually these files are written by a python script from a template
|
|
||||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
||||||
*.manifest
|
|
||||||
*.spec
|
|
||||||
|
|
||||||
# Installer logs
|
|
||||||
pip-log.txt
|
|
||||||
pip-delete-this-directory.txt
|
|
||||||
|
|
||||||
# Unit test / coverage reports
|
|
||||||
htmlcov/
|
|
||||||
.tox/
|
.tox/
|
||||||
.nox/
|
|
||||||
.coverage
|
.coverage
|
||||||
.coverage.*
|
|
||||||
.cache
|
|
||||||
nosetests.xml
|
|
||||||
coverage.xml
|
coverage.xml
|
||||||
*.cover
|
|
||||||
*.py,cover
|
|
||||||
.hypothesis/
|
|
||||||
.pytest_cache/
|
|
||||||
|
|
||||||
# Translations
|
|
||||||
*.mo
|
|
||||||
*.pot
|
|
||||||
|
|
||||||
# Django stuff:
|
|
||||||
*.log
|
|
||||||
local_settings.py
|
|
||||||
db.sqlite3
|
|
||||||
db.sqlite3-journal
|
|
||||||
|
|
||||||
# Flask stuff:
|
|
||||||
instance/
|
|
||||||
.webassets-cache
|
|
||||||
|
|
||||||
# Scrapy stuff:
|
|
||||||
.scrapy
|
|
||||||
|
|
||||||
# Sphinx documentation
|
|
||||||
docs/_build/
|
|
||||||
|
|
||||||
# PyBuilder
|
|
||||||
target/
|
|
||||||
|
|
||||||
# Jupyter Notebook
|
|
||||||
.ipynb_checkpoints
|
|
||||||
|
|
||||||
# IPython
|
|
||||||
profile_default/
|
|
||||||
ipython_config.py
|
|
||||||
|
|
||||||
# pyenv
|
|
||||||
.python-version
|
|
||||||
|
|
||||||
# pipenv
|
|
||||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
||||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
||||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
||||||
# install all needed dependencies.
|
|
||||||
#Pipfile.lock
|
|
||||||
|
|
||||||
# pyflow
|
|
||||||
__pypackages__/
|
|
||||||
|
|
||||||
# Celery stuff
|
|
||||||
celerybeat-schedule
|
|
||||||
celerybeat.pid
|
|
||||||
|
|
||||||
# SageMath parsed files
|
|
||||||
*.sage.py
|
|
||||||
|
|
||||||
# Environments
|
|
||||||
.env
|
|
||||||
.venv
|
|
||||||
env/
|
|
||||||
venv/
|
|
||||||
ENV/
|
|
||||||
env.bak/
|
|
||||||
venv.bak/
|
|
||||||
|
|
||||||
# Spyder project settings
|
|
||||||
.spyderproject
|
|
||||||
.spyproject
|
|
||||||
|
|
||||||
# Rope project settings
|
|
||||||
.ropeproject
|
|
||||||
|
|
||||||
# mkdocs documentation
|
|
||||||
/site
|
|
||||||
|
|
||||||
# mypy
|
|
||||||
.mypy_cache/
|
|
||||||
.dmypy.json
|
|
||||||
dmypy.json
|
|
||||||
|
|
||||||
# Pyre type checker
|
|
||||||
.pyre/
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
[style]
|
|
||||||
based_on_style = pep8
|
|
||||||
42
README.md
42
README.md
@@ -1,5 +1,9 @@
|
|||||||
# cmake-language-server
|
# cmake-language-server
|
||||||
|
[](https://pypi.org/project/cmake-language-server)
|
||||||
|
[](https://aur.archlinux.org/packages/cmake-language-server/)
|
||||||
[](https://github.com/regen100/cmake-language-server/actions)
|
[](https://github.com/regen100/cmake-language-server/actions)
|
||||||
|
[](https://codecov.io/gh/regen100/cmake-language-server)
|
||||||
|
[](https://github.com/regen100/cmake-language-server/blob/master/LICENSE)
|
||||||
|
|
||||||
CMake LSP Implementation.
|
CMake LSP Implementation.
|
||||||
|
|
||||||
@@ -12,17 +16,23 @@ Alpha Stage, work in progress.
|
|||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
|
|
||||||
- cmake-language-server: LSP server
|
- `cmake-language-server`: LSP server
|
||||||
- cmake-format: CLI frontend for formatting
|
- `cmake-format`: CLI frontend for formatting
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
### Clients
|
```bash
|
||||||
|
$ pip install cmake-language-server
|
||||||
|
```
|
||||||
|
|
||||||
- Neovim ([neoclide/coc.nvim][coc.nvim])
|
### Tested Clients
|
||||||
|
|
||||||
|
- Neovim ([neoclide/coc.nvim][coc.nvim], [prabirshrestha/vim-lsp][vim-lsp])
|
||||||
|
|
||||||
#### Neovim
|
#### Neovim
|
||||||
|
|
||||||
|
##### coc.nvim
|
||||||
|
|
||||||
```jsonc
|
```jsonc
|
||||||
"languageserver": {
|
"languageserver": {
|
||||||
"cmake": {
|
"cmake": {
|
||||||
@@ -38,5 +48,29 @@ Alpha Stage, work in progress.
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##### vim-lsp
|
||||||
|
|
||||||
|
```vim
|
||||||
|
if executable('cmake-language-server')
|
||||||
|
au User lsp_setup call lsp#register_server({
|
||||||
|
\ 'name': 'cmake',
|
||||||
|
\ 'cmd': {server_info->['cmake-language-server']},
|
||||||
|
\ 'root_uri': {server_info->lsp#utils#path_to_uri(lsp#utils#find_nearest_parent_file_directory(lsp#utils#get_buffer_path(), 'build/'))},
|
||||||
|
\ 'whitelist': ['cmake'],
|
||||||
|
\ 'initialization_options': {
|
||||||
|
\ 'buildDirectory': 'build',
|
||||||
|
\ }
|
||||||
|
\})
|
||||||
|
endif
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
* `buildDirectory`
|
||||||
|
This language server uses CMake's file API to get cached variables.
|
||||||
|
The API communicates using `<buildDirectory>/.cmake/api/`.
|
||||||
|
`buildDirectory` is relative path to the root uri of the workspace.
|
||||||
|
To configure the build tree, you need to run the cmake command such as `cmake .. -DFOO=bar`.
|
||||||
|
|
||||||
|
|
||||||
[coc.nvim]: https://github.com/neoclide/coc.nvim
|
[coc.nvim]: https://github.com/neoclide/coc.nvim
|
||||||
|
[vim-lsp]: https://github.com/prabirshrestha/vim-lsp
|
||||||
|
|||||||
7
codecov.yml
Normal file
7
codecov.yml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
coverage:
|
||||||
|
status:
|
||||||
|
project:
|
||||||
|
default:
|
||||||
|
threshold: 10%
|
||||||
|
patch:
|
||||||
|
default: off
|
||||||
6
mypy.ini
6
mypy.ini
@@ -1,6 +0,0 @@
|
|||||||
[mypy]
|
|
||||||
ignore_missing_imports = True
|
|
||||||
allow_redefinition = True
|
|
||||||
|
|
||||||
[mypy-re.Scanner]
|
|
||||||
ignore_errors = True
|
|
||||||
910
poetry.lock
generated
910
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "cmake-language-server"
|
name = "cmake-language-server"
|
||||||
version = "0.1.0"
|
version = "0.1.3"
|
||||||
description = "CMake LSP Implementation"
|
description = "CMake LSP Implementation"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
authors = ["regen"]
|
authors = ["regen"]
|
||||||
@@ -19,22 +19,35 @@ classifiers = [
|
|||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = "^3.6"
|
python = "^3.6"
|
||||||
pygls = "^0.8.1"
|
pygls = "^0.11"
|
||||||
pyparsing = "^2.4"
|
pyparsing = "^2.4"
|
||||||
|
importlib-metadata = {version = "^4.8", python = "<3.8"}
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
flake8 = "^3.7"
|
pytest = "^6.2"
|
||||||
mypy = "^0.740.0"
|
|
||||||
pytest = "^5.2"
|
|
||||||
yapf = "^0.28.0"
|
|
||||||
pytest-datadir = "^1.3"
|
pytest-datadir = "^1.3"
|
||||||
tox = "^3.14"
|
pytest-cov = "^2.11"
|
||||||
isort = "^4.3"
|
pysen = {version = "^0.9", extras = ["lint"]}
|
||||||
|
|
||||||
[tool.poetry.scripts]
|
[tool.poetry.scripts]
|
||||||
cmake-format = "cmake_language_server.formatter:main"
|
cmake-format = "cmake_language_server.formatter:main"
|
||||||
cmake-language-server = "cmake_language_server.server:main"
|
cmake-language-server = "cmake_language_server.server:main"
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["poetry>=0.12"]
|
requires = ["poetry-core>=1.0.0"]
|
||||||
build-backend = "poetry.masonry.api"
|
build-backend = "poetry.core.masonry.api"
|
||||||
|
|
||||||
|
[tool.pysen]
|
||||||
|
version = "0.9"
|
||||||
|
|
||||||
|
[tool.pysen.lint]
|
||||||
|
enable_black = true
|
||||||
|
enable_flake8 = true
|
||||||
|
enable_isort = true
|
||||||
|
enable_mypy = true
|
||||||
|
mypy_preset = "strict"
|
||||||
|
line_length = 88
|
||||||
|
py_version = "py36"
|
||||||
|
|
||||||
|
[[tool.pysen.lint.mypy_targets]]
|
||||||
|
paths = ["."]
|
||||||
|
|||||||
@@ -1 +1,6 @@
|
|||||||
__version__ = '0.1.0'
|
try:
|
||||||
|
import importlib.metadata as importlib_metadata
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
import importlib_metadata
|
||||||
|
|
||||||
|
__version__ = importlib_metadata.version(__name__)
|
||||||
|
|||||||
@@ -10,13 +10,23 @@ from typing import Dict, List, Optional, Pattern
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _tidy_doc(doc: str) -> str:
|
||||||
|
doc = doc.strip()
|
||||||
|
doc = re.sub(r":.+?:`(.+?)`", r"\1", doc)
|
||||||
|
doc = re.sub(r"``([^`]+)``", r"`\1`", doc)
|
||||||
|
doc = doc.replace("\n", " ")
|
||||||
|
doc = doc.replace(". ", ". ")
|
||||||
|
return doc
|
||||||
|
|
||||||
|
|
||||||
class API(object):
|
class API(object):
|
||||||
_cmake: str
|
_cmake: str
|
||||||
_build: Path
|
_build: Path
|
||||||
_uuid: uuid.UUID
|
_uuid: uuid.UUID
|
||||||
_builtin_commands: Dict[str, str]
|
_builtin_commands: Dict[str, str]
|
||||||
_builtin_variables: Dict[str, str]
|
_builtin_variables: Dict[str, str]
|
||||||
_builtin_variable_template: Dict[Pattern, str]
|
_builtin_variable_template: Dict[Pattern[str], str]
|
||||||
|
_builtin_modules: Dict[str, str]
|
||||||
_targets: List[str]
|
_targets: List[str]
|
||||||
_cached_variables: Dict[str, str]
|
_cached_variables: Dict[str, str]
|
||||||
_generated_list_parsed: bool
|
_generated_list_parsed: bool
|
||||||
@@ -29,86 +39,103 @@ class API(object):
|
|||||||
self._builtin_commands = {}
|
self._builtin_commands = {}
|
||||||
self._builtin_variables = {}
|
self._builtin_variables = {}
|
||||||
self._builtin_variable_template = {}
|
self._builtin_variable_template = {}
|
||||||
|
self._builtin_modules = {}
|
||||||
self._targets = []
|
self._targets = []
|
||||||
self._cached_variables = {}
|
self._cached_variables = {}
|
||||||
self._generated_list_parsed = False
|
self._generated_list_parsed = False
|
||||||
|
|
||||||
def query(self) -> bool:
|
def query(self) -> bool:
|
||||||
|
"""Use CMake's file API to get JSON information about the build tree
|
||||||
|
|
||||||
|
Generates a JSON request file for the current build tree and runs
|
||||||
|
CMake on the build tree. Deletes the request file immediately
|
||||||
|
after.
|
||||||
|
"""
|
||||||
if not self.cmake_cache.exists():
|
if not self.cmake_cache.exists():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.query_json.parent.mkdir(parents=True, exist_ok=True)
|
self.query_json.parent.mkdir(parents=True, exist_ok=True)
|
||||||
with self.query_json.open('w') as fp:
|
with self.query_json.open("w") as fp:
|
||||||
fp.write('''\
|
fp.write(
|
||||||
|
"""\
|
||||||
{
|
{
|
||||||
"requests": [
|
"requests": [
|
||||||
{"kind": "codemodel", "version": 2},
|
{"kind": "codemodel", "version": 2},
|
||||||
{"kind": "cache", "version": 2},
|
{"kind": "cache", "version": 2},
|
||||||
{"kind": "cmakeFiles", "version": 1}
|
{"kind": "cmakeFiles", "version": 1}
|
||||||
]
|
]
|
||||||
}''')
|
}"""
|
||||||
|
)
|
||||||
|
|
||||||
proc = subprocess.run([self._cmake, self._build],
|
proc = subprocess.run(
|
||||||
universal_newlines=True,
|
[self._cmake, str(self._build)],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE)
|
stderr=subprocess.PIPE,
|
||||||
|
encoding="utf-8",
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
self.query_json.unlink()
|
self.query_json.unlink()
|
||||||
self.query_json.parent.rmdir()
|
self.query_json.parent.rmdir()
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
logging.error(
|
logging.error(f"cmake exited with {proc.returncode}: {proc.stderr}")
|
||||||
f'cmake exited with {proc.returncode}: {proc.stderr}')
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def read_reply(self) -> bool:
|
def read_reply(self) -> bool:
|
||||||
reply = self._build / '.cmake' / 'api' / 'v1' / 'reply'
|
"""Reads the CMake file API reply file and updates internal state
|
||||||
indices = sorted(reply.glob('index-*.json'))
|
|
||||||
|
Reads the result of the previous query file and updates
|
||||||
|
the targets, the cache entries and the cmake files.
|
||||||
|
"""
|
||||||
|
reply = self._build / ".cmake" / "api" / "v1" / "reply"
|
||||||
|
indices = sorted(reply.glob("index-*.json"))
|
||||||
if not indices:
|
if not indices:
|
||||||
logger.error('no reply')
|
logger.error("no reply")
|
||||||
return False
|
return False
|
||||||
with indices[-1].open() as fp:
|
with indices[-1].open() as fp:
|
||||||
index = json.load(fp)
|
index = json.load(fp)
|
||||||
try:
|
try:
|
||||||
responses = index['reply'][f'client-{self._uuid}']['query.json'][
|
responses = index["reply"][f"client-{self._uuid}"]["query.json"][
|
||||||
'responses']
|
"responses"
|
||||||
|
]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
logger.error('no rensponse')
|
logger.error("no rensponse")
|
||||||
return False
|
return False
|
||||||
for response in responses:
|
for response in responses:
|
||||||
if response['kind'] == 'codemodel':
|
if response["kind"] == "codemodel":
|
||||||
self._read_codemodel(reply / response['jsonFile'])
|
self._read_codemodel(reply / response["jsonFile"])
|
||||||
elif response['kind'] == 'cache':
|
elif response["kind"] == "cache":
|
||||||
self._read_cache(reply / response['jsonFile'])
|
self._read_cache(reply / response["jsonFile"])
|
||||||
elif response['kind'] == 'cmakeFiles':
|
elif response["kind"] == "cmakeFiles":
|
||||||
self._read_cmake_files(reply / response['jsonFile'])
|
self._read_cmake_files(reply / response["jsonFile"])
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _read_codemodel(self, codemodelpath: Path):
|
def _read_codemodel(self, codemodelpath: Path) -> None:
|
||||||
with (codemodelpath).open() as fp:
|
with (codemodelpath).open() as fp:
|
||||||
codemodel = json.load(fp)
|
codemodel = json.load(fp)
|
||||||
config = codemodel['configurations'][0]
|
config = codemodel["configurations"][0]
|
||||||
self._targets[:] = [x['name'] for x in config['targets']]
|
self._targets[:] = [x["name"] for x in config["targets"]]
|
||||||
|
|
||||||
def _read_cache(self, cachepath: Path):
|
def _read_cache(self, cachepath: Path) -> None:
|
||||||
with cachepath.open() as fp:
|
with cachepath.open() as fp:
|
||||||
cache = json.load(fp)
|
cache = json.load(fp)
|
||||||
self._cached_variables.clear()
|
self._cached_variables.clear()
|
||||||
for entry in cache['entries']:
|
for entry in cache["entries"]:
|
||||||
name = entry['name']
|
name = entry["name"]
|
||||||
value = self._truncate_variable(entry['value'])
|
value = self._truncate_variable(entry["value"])
|
||||||
properties = {x['name']: x['value'] for x in entry['properties']}
|
properties = {x["name"]: x["value"] for x in entry["properties"]}
|
||||||
helpstring = properties.get('HELPSTRING', '')
|
helpstring = properties.get("HELPSTRING", "")
|
||||||
doc = []
|
doc = []
|
||||||
if helpstring:
|
if helpstring:
|
||||||
doc.append(helpstring)
|
doc.append(helpstring)
|
||||||
if value:
|
if value:
|
||||||
doc.append(f'`{value}`')
|
doc.append(f"`{value}`")
|
||||||
self._cached_variables[name] = '\n\n'.join(doc)
|
self._cached_variables[name] = "\n\n".join(doc)
|
||||||
|
|
||||||
def _read_cmake_files(self, jsonpath: Path):
|
def _read_cmake_files(self, jsonpath: Path) -> None:
|
||||||
'''inspect generated list files'''
|
"""inspect CMake list files that are used during build generation"""
|
||||||
|
|
||||||
if not self._builtin_variables or self._generated_list_parsed:
|
if not self._builtin_variables or self._generated_list_parsed:
|
||||||
return
|
return
|
||||||
@@ -116,44 +143,48 @@ class API(object):
|
|||||||
with jsonpath.open() as fp:
|
with jsonpath.open() as fp:
|
||||||
cmake_files = json.load(fp)
|
cmake_files = json.load(fp)
|
||||||
|
|
||||||
# inspect generated list files
|
# Inspect generated list files: Get the values of variables in each script
|
||||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
tmplist = Path(tmpdirname) / 'dump.cmake'
|
tmplist = Path(tmpdirname) / "dump.cmake"
|
||||||
with tmplist.open('w') as fp:
|
with tmplist.open("w") as fp:
|
||||||
for listfile in cmake_files['inputs']:
|
for listfile in cmake_files["inputs"]:
|
||||||
if not listfile.get('isGenerated', False):
|
if not listfile.get("isGenerated", False):
|
||||||
continue
|
continue
|
||||||
path = listfile['path']
|
path = listfile["path"]
|
||||||
fp.write(f'include({path})\n')
|
fp.write(f"include({path})\n")
|
||||||
fp.write('''
|
fp.write(
|
||||||
|
"""
|
||||||
get_cmake_property(variables VARIABLES)
|
get_cmake_property(variables VARIABLES)
|
||||||
foreach (variable ${variables})
|
foreach (variable ${variables})
|
||||||
message("${variable}=${${variable}}")
|
message("${variable}=${${variable}}")
|
||||||
endforeach()
|
endforeach()
|
||||||
''')
|
"""
|
||||||
p = subprocess.run([self._cmake, '-P', tmplist],
|
)
|
||||||
cwd=cmake_files['paths']['source'],
|
p = subprocess.run(
|
||||||
universal_newlines=True,
|
[self._cmake, "-P", str(tmplist)],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE)
|
stderr=subprocess.PIPE,
|
||||||
|
cwd=cmake_files["paths"]["source"],
|
||||||
|
encoding="utf-8",
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
if p.returncode != 0:
|
if p.returncode != 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
for line in p.stderr.split('\n'):
|
for line in p.stderr.split("\n"):
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
k, v = line.split('=', 1)
|
k, v = line.split("=", 1)
|
||||||
if k.startswith('CMAKE_ARG'):
|
if k.startswith("CMAKE_ARG"):
|
||||||
continue
|
continue
|
||||||
v = self._truncate_variable(v)
|
v = self._truncate_variable(v)
|
||||||
if k in self._builtin_variables:
|
if k in self._builtin_variables:
|
||||||
self._builtin_variables[k] += f'\n\n`{v}`'
|
self._builtin_variables[k] += f"\n\n`{v}`"
|
||||||
else:
|
else:
|
||||||
for pattern, doc in self._builtin_variable_template.items(
|
for pattern, doc in self._builtin_variable_template.items():
|
||||||
):
|
|
||||||
if pattern.fullmatch(k):
|
if pattern.fullmatch(k):
|
||||||
self._builtin_variables[k] = f'{doc}\n\n`{v}`'
|
self._builtin_variables[k] = f"{doc}\n\n`{v}`"
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# ignore variable with no document
|
# ignore variable with no document
|
||||||
@@ -163,72 +194,132 @@ endforeach()
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def query_json(self) -> Path:
|
def query_json(self) -> Path:
|
||||||
return (self._build / '.cmake' / 'api' / 'v1' / 'query' /
|
return (
|
||||||
f'client-{self._uuid}' / 'query.json')
|
self._build
|
||||||
|
/ ".cmake"
|
||||||
|
/ "api"
|
||||||
|
/ "v1"
|
||||||
|
/ "query"
|
||||||
|
/ f"client-{self._uuid}"
|
||||||
|
/ "query.json"
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cmake_cache(self) -> Path:
|
def cmake_cache(self) -> Path:
|
||||||
return self._build / 'CMakeCache.txt'
|
return self._build / "CMakeCache.txt"
|
||||||
|
|
||||||
def parse_doc(self) -> None:
|
def parse_doc(self) -> None:
|
||||||
self._parse_commands()
|
self._parse_commands()
|
||||||
self._parse_variables()
|
self._parse_variables()
|
||||||
|
self._parse_modules()
|
||||||
|
|
||||||
def _parse_commands(self) -> None:
|
def _parse_commands(self) -> None:
|
||||||
p = subprocess.run([self._cmake, '--help-commands'],
|
"""Load docs for builtin cmake functions
|
||||||
|
|
||||||
|
Loads the documentation for builtin cmake functions from the result
|
||||||
|
of `$ cmake --help-commands`.
|
||||||
|
"""
|
||||||
|
p = subprocess.run(
|
||||||
|
[self._cmake, "--help-commands"],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
universal_newlines=True)
|
encoding="utf-8",
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
|
|
||||||
if p.returncode != 0:
|
if p.returncode != 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
matches = re.finditer(
|
matches = re.finditer(
|
||||||
r'''
|
r"""
|
||||||
(?P<command>.+)\n
|
(?P<command>.+)\n
|
||||||
-+\n\n
|
-+\n+?
|
||||||
[\s\S]*?
|
[\s\S]*?
|
||||||
(?P<signature>\ (?P=command)\s*\([^)]*\))
|
(?P<signature>(?P=command)\s*\([^)]*\))
|
||||||
''', p.stdout, re.VERBOSE)
|
""",
|
||||||
|
p.stdout,
|
||||||
|
re.VERBOSE,
|
||||||
|
)
|
||||||
self._builtin_commands.clear()
|
self._builtin_commands.clear()
|
||||||
for match in matches:
|
for match in matches:
|
||||||
command = match.group('command')
|
command = match.group("command")
|
||||||
signature = match.group('signature')
|
signature = match.group("signature")
|
||||||
signature = re.sub(r'^ ', r'', signature, flags=re.MULTILINE)
|
signature = re.sub(r"^ ", r"", signature, flags=re.MULTILINE)
|
||||||
self._builtin_commands[
|
self._builtin_commands[command] = "```cmake\n" + signature + "\n```"
|
||||||
command] = '```cmake\n' + signature + '\n```'
|
|
||||||
|
|
||||||
def _parse_variables(self) -> None:
|
def _parse_variables(self) -> None:
|
||||||
p = subprocess.run([self._cmake, '--help-variables'],
|
"""Load docs for builtin cmake variables
|
||||||
|
|
||||||
|
Loads the documentation for builtin cmake variables from
|
||||||
|
the result of `$ cmake --help-variables`.
|
||||||
|
"""
|
||||||
|
p = subprocess.run(
|
||||||
|
[self._cmake, "--help-variables"],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
universal_newlines=True)
|
encoding="utf-8",
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
|
|
||||||
if p.returncode != 0:
|
if p.returncode != 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
matches = re.finditer(
|
matches = re.finditer(
|
||||||
r'''
|
r"""
|
||||||
(?P<variable>.+)\n
|
(?P<variable>.+)\n
|
||||||
-+\n\n
|
-+\n\n
|
||||||
(?P<doc>[\s\S]+?)(?:\n\n|$)
|
(?P<doc>[\s\S]+?)(?:\n\n|$)
|
||||||
''', p.stdout, re.VERBOSE)
|
""",
|
||||||
|
p.stdout,
|
||||||
|
re.VERBOSE,
|
||||||
|
)
|
||||||
self._builtin_variables.clear()
|
self._builtin_variables.clear()
|
||||||
for match in matches:
|
for match in matches:
|
||||||
variable = match.group('variable')
|
variable = match.group("variable")
|
||||||
doc = match.group('doc')
|
doc = _tidy_doc(match.group("doc"))
|
||||||
doc = re.sub(r':.+:`(.+)`', r'\1', doc)
|
if variable == "CMAKE_MATCH_<n>":
|
||||||
doc = re.sub(r'``(.+)``', r'`\1`', doc)
|
|
||||||
doc = doc.replace('\n', ' ')
|
|
||||||
doc = doc.replace('. ', '. ')
|
|
||||||
if variable == 'CMAKE_MATCH_<n>':
|
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
self._builtin_variables[f'CMAKE_MATCH_{i}'] = doc
|
self._builtin_variables[f"CMAKE_MATCH_{i}"] = doc
|
||||||
elif '<' in variable:
|
elif "<" in variable:
|
||||||
variable = re.sub(r'<[^>]+>', r'[^_]+', variable)
|
variable = re.sub(r"<[^>]+>", r"[^_]+", variable)
|
||||||
pattern = re.compile(variable)
|
pattern = re.compile(variable)
|
||||||
self._builtin_variable_template[pattern] = doc
|
self._builtin_variable_template[pattern] = doc
|
||||||
else:
|
else:
|
||||||
self._builtin_variables[variable] = doc
|
self._builtin_variables[variable] = doc
|
||||||
|
|
||||||
|
def _parse_modules(self) -> None:
|
||||||
|
"""Loads docs for all modules in the cmake distribution
|
||||||
|
|
||||||
|
Loads the documentation for cmake modules included in the
|
||||||
|
distribution from the result of `$ cmake --help-modules`.
|
||||||
|
"""
|
||||||
|
p = subprocess.run(
|
||||||
|
[self._cmake, "--help-modules"],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
encoding="utf-8",
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
if p.returncode != 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
matches = re.finditer(
|
||||||
|
r"""
|
||||||
|
(?P<module>.+)\n
|
||||||
|
-+\n+?
|
||||||
|
(?:(?P<header>\w[\w\s]+)\n\^+\n+?)?
|
||||||
|
(?P<doc>(?:.|\n)*?\n\n)
|
||||||
|
""",
|
||||||
|
p.stdout + "\n\n",
|
||||||
|
re.VERBOSE,
|
||||||
|
)
|
||||||
|
self._builtin_modules.clear()
|
||||||
|
for match in matches:
|
||||||
|
module = match.group("module")
|
||||||
|
header = match.group("header")
|
||||||
|
doc = _tidy_doc(match.group("doc"))
|
||||||
|
if header != "Overview":
|
||||||
|
doc = ""
|
||||||
|
self._builtin_modules[module] = doc
|
||||||
|
|
||||||
def get_command_doc(self, command: str) -> Optional[str]:
|
def get_command_doc(self, command: str) -> Optional[str]:
|
||||||
return self._builtin_commands.get(command)
|
return self._builtin_commands.get(command)
|
||||||
|
|
||||||
@@ -243,15 +334,32 @@ endforeach()
|
|||||||
return self._builtin_variables.get(variable)
|
return self._builtin_variables.get(variable)
|
||||||
|
|
||||||
def search_variable(self, variable: str) -> List[str]:
|
def search_variable(self, variable: str) -> List[str]:
|
||||||
cached = frozenset(x for x in self._cached_variables
|
cached = frozenset(x for x in self._cached_variables if x.startswith(variable))
|
||||||
if x.startswith(variable))
|
builtin = frozenset(
|
||||||
builtin = frozenset(x for x in self._builtin_variables
|
x for x in self._builtin_variables if x.startswith(variable)
|
||||||
if x.startswith(variable))
|
)
|
||||||
return list(cached | builtin)
|
return list(cached | builtin)
|
||||||
|
|
||||||
|
def get_module_doc(self, module: str, package: bool) -> Optional[str]:
|
||||||
|
if package:
|
||||||
|
return self._builtin_modules.get("Find" + module)
|
||||||
|
|
||||||
|
return self._builtin_modules.get(module)
|
||||||
|
|
||||||
|
def search_module(self, module: str, package: bool) -> List[str]:
|
||||||
|
if package:
|
||||||
|
module = "Find" + module
|
||||||
|
return [x[4:] for x in self._builtin_modules if x.startswith(module)]
|
||||||
|
|
||||||
|
return [
|
||||||
|
x
|
||||||
|
for x in self._builtin_modules
|
||||||
|
if x.startswith(module) and not x.startswith("Find")
|
||||||
|
]
|
||||||
|
|
||||||
def search_target(self, target: str) -> List[str]:
|
def search_target(self, target: str) -> List[str]:
|
||||||
return [x for x in self._targets if x.startswith(target)]
|
return [x for x in self._targets if x.startswith(target)]
|
||||||
|
|
||||||
def _truncate_variable(self, v: str) -> str:
|
def _truncate_variable(self, v: str) -> str:
|
||||||
width = 70
|
width = 70
|
||||||
return v[:width] + (v[width:] and '...')
|
return v[:width] + (v[width:] and "...")
|
||||||
|
|||||||
@@ -1,106 +1,137 @@
|
|||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from .parser import TokenList
|
from .parser import TokenList
|
||||||
|
|
||||||
|
|
||||||
class Formatter(object):
|
class Formatter(object):
|
||||||
indnt: str
|
indent: str
|
||||||
lower_identifier: bool
|
lower_identifier: bool
|
||||||
|
|
||||||
def __init__(self, indent=' ', lower_identifier=True):
|
def __init__(self, indent: str = " ", lower_identifier: bool = True):
|
||||||
self.indent = indent
|
self.indent = indent
|
||||||
self.lower_identifier = lower_identifier
|
self.lower_identifier = lower_identifier
|
||||||
|
|
||||||
def format(self, tokens: TokenList) -> str:
|
def format(self, tokens: TokenList) -> str:
|
||||||
cmds: List[str] = ['']
|
cmds: List[str] = [""]
|
||||||
indnet_level = 0
|
indent_level = 0
|
||||||
for token in tokens:
|
for token in tokens:
|
||||||
if isinstance(token, tuple):
|
if isinstance(token, tuple):
|
||||||
raw_identifier = token[0]
|
raw_identifier = token[0]
|
||||||
identifier = raw_identifier.lower()
|
identifier = raw_identifier.lower()
|
||||||
if identifier in ('elseif', 'else', 'endif', 'endforeach',
|
if identifier in (
|
||||||
'endwhile', 'endmacro', 'endfunction'):
|
"elseif",
|
||||||
if indnet_level > 0:
|
"else",
|
||||||
indnet_level -= 1
|
"endif",
|
||||||
cmds[-1] = self.indent * indnet_level
|
"endforeach",
|
||||||
cmds[-1] += (identifier
|
"endwhile",
|
||||||
if self.lower_identifier else raw_identifier)
|
"endmacro",
|
||||||
|
"endfunction",
|
||||||
|
):
|
||||||
|
if indent_level > 0:
|
||||||
|
indent_level -= 1
|
||||||
|
cmds[-1] = self.indent * indent_level
|
||||||
|
cmds[-1] += identifier if self.lower_identifier else raw_identifier
|
||||||
args = self._format_args(token[1])
|
args = self._format_args(token[1])
|
||||||
if len(args) < 2:
|
if len(args) < 2:
|
||||||
cmds[-1] += '(' + ''.join(args) + ')'
|
cmds[-1] += "(" + "".join(args) + ")"
|
||||||
else:
|
else:
|
||||||
cmds[-1] += '(\n'
|
cmds[-1] += "(\n"
|
||||||
for arg in args:
|
for arg in args:
|
||||||
cmds[-1] += self.indent * (indnet_level +
|
cmds[-1] += self.indent * (indent_level + 1) + arg + "\n"
|
||||||
1) + arg + '\n'
|
cmds[-1] += self.indent * indent_level + ")"
|
||||||
cmds[-1] += self.indent * indnet_level + ')'
|
if identifier in (
|
||||||
if identifier in ('if', 'elseif', 'else', 'foreach', 'while',
|
"if",
|
||||||
'macro', 'function'):
|
"elseif",
|
||||||
indnet_level += 1
|
"else",
|
||||||
elif token == '\n':
|
"foreach",
|
||||||
cmds.append('')
|
"while",
|
||||||
elif token[0] == '#':
|
"macro",
|
||||||
|
"function",
|
||||||
|
):
|
||||||
|
indent_level += 1
|
||||||
|
elif token == "\n":
|
||||||
|
cmds.append("")
|
||||||
|
elif token[0] == "#":
|
||||||
if cmds[-1]:
|
if cmds[-1]:
|
||||||
cmds[-1] += token
|
cmds[-1] += token
|
||||||
else:
|
else:
|
||||||
cmds[-1] = self.indent * indnet_level + token
|
cmds[-1] = self.indent * indent_level + token
|
||||||
elif cmds[-1]:
|
elif cmds[-1]:
|
||||||
cmds[-1] += token
|
cmds[-1] += token
|
||||||
|
|
||||||
cmds = self._strip_line(cmds)
|
cmds = self._strip_line(cmds)
|
||||||
return '\n'.join(cmds) + '\n'
|
return "\n".join(cmds) + "\n"
|
||||||
|
|
||||||
def _format_args(self, args: List[str]) -> List[str]:
|
def _format_args(self, args: List[str]) -> List[str]:
|
||||||
lines = ['']
|
lines = [""]
|
||||||
for i in range(len(args)):
|
for i in range(len(args)):
|
||||||
arg = args[i]
|
arg = args[i]
|
||||||
if arg[0] == '#':
|
if arg[0] == "#":
|
||||||
lines[-1] += arg
|
lines[-1] += arg
|
||||||
elif arg[0] == '\n':
|
elif arg[0] == "\n":
|
||||||
lines.append('')
|
lines.append("")
|
||||||
elif arg.isspace():
|
elif arg.isspace():
|
||||||
if lines[-1]:
|
if lines[-1]:
|
||||||
if i + 1 < len(args) and args[i + 1][0] == '#':
|
if i + 1 < len(args) and args[i + 1][0] == "#":
|
||||||
lines[-1] += arg
|
lines[-1] += arg
|
||||||
else:
|
else:
|
||||||
lines[-1] += ' '
|
lines[-1] += " "
|
||||||
else:
|
else:
|
||||||
lines[-1] += arg
|
lines[-1] += arg
|
||||||
|
|
||||||
return self._strip_line(lines)
|
return self._strip_line(lines)
|
||||||
|
|
||||||
def _strip_line(self, lines: List[str]) -> List[str]:
|
def _strip_line(self, lines: List[str]) -> List[str]:
|
||||||
'''Delete empty lines at the start/end of the input'''
|
"""Delete empty lines at the start/end of the input"""
|
||||||
|
|
||||||
ret: List[str] = []
|
ret: List[str] = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
if line != '' or len(ret) > 0:
|
if line != "" or len(ret) > 0:
|
||||||
ret.append(line)
|
ret.append(line)
|
||||||
while ret and ret[-1] == '':
|
while ret and ret[-1] == "":
|
||||||
del ret[-1]
|
del ret[-1]
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def main(args: List[str] = None):
|
def main(argss: Optional[List[str]] = None) -> None:
|
||||||
from pathlib import Path
|
import sys
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from .parser import ListParser
|
|
||||||
from difflib import unified_diff
|
from difflib import unified_diff
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
parser = ArgumentParser()
|
from . import __version__
|
||||||
parser.add_argument('lists', type=Path, nargs='*', help='CMake list files')
|
from .parser import ListParser
|
||||||
parser.add_argument('-i',
|
|
||||||
'--inplace',
|
|
||||||
action='store_true',
|
|
||||||
help='inplace edit')
|
|
||||||
parser.add_argument('-d', '--diff', action='store_true', help='show diff')
|
|
||||||
|
|
||||||
args = parser.parse_args(args)
|
parser = ArgumentParser(
|
||||||
|
description="Format CMake list files.",
|
||||||
|
epilog="""
|
||||||
|
If no arguments are specified, it formats the code from
|
||||||
|
standard input and writes the result to the standard output.""",
|
||||||
|
)
|
||||||
|
parser.add_argument("lists", type=Path, nargs="*", help="CMake list files")
|
||||||
|
group = parser.add_mutually_exclusive_group()
|
||||||
|
group.add_argument("-i", "--inplace", action="store_true", help="inplace edit")
|
||||||
|
group.add_argument("-d", "--diff", action="store_true", help="show diff")
|
||||||
|
parser.add_argument(
|
||||||
|
"--version", action="version", version=f"%(prog)s {__version__}"
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args(argss)
|
||||||
|
|
||||||
|
if not args.lists and args.inplace:
|
||||||
|
print("error: cannot use -i when no arguments are specified.", file=sys.stderr)
|
||||||
|
return
|
||||||
|
if not args.lists:
|
||||||
|
args.lists.append(None)
|
||||||
|
|
||||||
list_parser = ListParser()
|
list_parser = ListParser()
|
||||||
formatter = Formatter()
|
formatter = Formatter()
|
||||||
for listpath in args.lists:
|
for listpath in args.lists:
|
||||||
|
if listpath is None:
|
||||||
|
listpath = "(stdin)"
|
||||||
|
content = sys.stdin.read()
|
||||||
|
else:
|
||||||
with listpath.open() as fp:
|
with listpath.open() as fp:
|
||||||
content = fp.read()
|
content = fp.read()
|
||||||
tokens, remain = list_parser.parse(content)
|
tokens, remain = list_parser.parse(content)
|
||||||
@@ -108,16 +139,18 @@ def main(args: List[str] = None):
|
|||||||
|
|
||||||
if args.inplace:
|
if args.inplace:
|
||||||
if not remain:
|
if not remain:
|
||||||
with listpath.open('w') as fp:
|
with listpath.open("w") as fp:
|
||||||
fp.write(formatted)
|
fp.write(formatted)
|
||||||
|
elif args.diff:
|
||||||
|
diff = unified_diff(
|
||||||
|
content.splitlines(True),
|
||||||
|
formatted.splitlines(True),
|
||||||
|
str(listpath),
|
||||||
|
str(listpath),
|
||||||
|
"(before formatting)",
|
||||||
|
"(after formatting)",
|
||||||
|
)
|
||||||
|
diffstr = "".join(diff)
|
||||||
|
print(diffstr, end="")
|
||||||
else:
|
else:
|
||||||
if args.diff:
|
print(formatted, end="")
|
||||||
diff = unified_diff(content.splitlines(True),
|
|
||||||
formatted.splitlines(True), str(listpath),
|
|
||||||
str(listpath), '(before formatting)',
|
|
||||||
'(after formatting)')
|
|
||||||
diffstr = ''.join(diff)
|
|
||||||
if diffstr:
|
|
||||||
print(diffstr, end='')
|
|
||||||
else:
|
|
||||||
print(formatted, end='')
|
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ TokenList = List[TokenType]
|
|||||||
class ListParser(object):
|
class ListParser(object):
|
||||||
_parser: pp.ParserElement
|
_parser: pp.ParserElement
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
newline = '\n'
|
newline = "\n"
|
||||||
space_plus = pp.Regex('[ \t]+')
|
space_plus = pp.Regex("[ \t]+")
|
||||||
space_star = pp.Optional(space_plus)
|
space_star = pp.Optional(space_plus)
|
||||||
|
|
||||||
quoted_element = pp.Regex(r'[^\\"]|\\[^A-Za-z0-9]|\\[trn]')
|
quoted_element = pp.Regex(r'[^\\"]|\\[^A-Za-z0-9]|\\[trn]')
|
||||||
@@ -20,12 +20,12 @@ class ListParser(object):
|
|||||||
|
|
||||||
bracket_content = pp.Forward()
|
bracket_content = pp.Forward()
|
||||||
|
|
||||||
def action_bracket_open(tokens: pp.ParseResults):
|
def action_bracket_open(tokens: pp.ParseResults) -> None:
|
||||||
nonlocal bracket_content
|
nonlocal bracket_content
|
||||||
marker = ']' + '=' * (len(tokens[0]) - 2) + ']'
|
marker = "]" + "=" * (len(tokens[0]) - 2) + "]"
|
||||||
bracket_content <<= pp.SkipTo(marker, include=True)
|
bracket_content <<= pp.SkipTo(marker, include=True)
|
||||||
|
|
||||||
bracket_open = pp.Regex(r'\[=*\[').setParseAction(action_bracket_open)
|
bracket_open = pp.Regex(r"\[=*\[").setParseAction(action_bracket_open)
|
||||||
bracket_argument = pp.Combine(bracket_open + bracket_content)
|
bracket_argument = pp.Combine(bracket_open + bracket_content)
|
||||||
|
|
||||||
unquoted_element = pp.Regex(r'[^\s()#"\\]|\\[^A-Za-z0-9]|\\[trn]')
|
unquoted_element = pp.Regex(r'[^\s()#"\\]|\\[^A-Za-z0-9]|\\[trn]')
|
||||||
@@ -33,25 +33,29 @@ class ListParser(object):
|
|||||||
|
|
||||||
argument = bracket_argument | quoted_argument | unquoted_argument
|
argument = bracket_argument | quoted_argument | unquoted_argument
|
||||||
|
|
||||||
line_comment = pp.Combine('#' + ~bracket_open +
|
line_comment = pp.Combine("#" + ~bracket_open + pp.SkipTo(pp.LineEnd()))
|
||||||
pp.SkipTo(pp.LineEnd()))
|
bracket_comment = pp.Combine("#" + bracket_argument)
|
||||||
bracket_comment = pp.Combine('#' + bracket_argument)
|
line_ending = (
|
||||||
line_ending = (space_star +
|
space_star
|
||||||
pp.ZeroOrMore(bracket_comment + space_star) +
|
+ pp.ZeroOrMore(bracket_comment + space_star)
|
||||||
pp.Optional(line_comment) + (newline | pp.lineEnd))
|
+ pp.Optional(line_comment)
|
||||||
|
+ (newline | pp.lineEnd)
|
||||||
|
)
|
||||||
|
|
||||||
identifier = pp.Word(pp.alphas + '_', pp.alphanums + '_')
|
identifier = pp.Word(pp.alphas + "_", pp.alphanums + "_")
|
||||||
arguments = pp.Forward()
|
arguments = pp.Forward()
|
||||||
arguments << pp.ZeroOrMore(argument | line_ending | space_plus
|
arguments << pp.ZeroOrMore(
|
||||||
| '(' + arguments + ')').leaveWhitespace()
|
argument | line_ending | space_plus | "(" + arguments + ")"
|
||||||
|
).leaveWhitespace()
|
||||||
arguments = pp.Group(arguments)
|
arguments = pp.Group(arguments)
|
||||||
PAREN_L, PAREN_R = map(pp.Suppress, '()')
|
PAREN_L, PAREN_R = map(pp.Suppress, "()")
|
||||||
command_invocation = (
|
command_invocation = (
|
||||||
identifier + space_star.suppress() + PAREN_L + arguments +
|
identifier + space_star.suppress() + PAREN_L + arguments + PAREN_R
|
||||||
PAREN_R).setParseAction(lambda t: (t[0], t[1].asList()))
|
).setParseAction(lambda t: (t[0], t[1].asList()))
|
||||||
|
|
||||||
file_element = (space_star + command_invocation + line_ending
|
file_element = (
|
||||||
| line_ending).leaveWhitespace()
|
space_star + command_invocation + line_ending | line_ending
|
||||||
|
).leaveWhitespace()
|
||||||
file = pp.ZeroOrMore(file_element)
|
file = pp.ZeroOrMore(file_element)
|
||||||
|
|
||||||
self._parser = file
|
self._parser = file
|
||||||
|
|||||||
@@ -1,16 +1,35 @@
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Optional, Tuple
|
from typing import Any, Callable, List, Optional, Tuple
|
||||||
|
|
||||||
from pygls.features import (COMPLETION, FORMATTING, HOVER, INITIALIZE,
|
from pygls.lsp.methods import (
|
||||||
INITIALIZED, TEXT_DOCUMENT_DID_SAVE)
|
COMPLETION,
|
||||||
|
FORMATTING,
|
||||||
|
HOVER,
|
||||||
|
INITIALIZE,
|
||||||
|
INITIALIZED,
|
||||||
|
TEXT_DOCUMENT_DID_SAVE,
|
||||||
|
)
|
||||||
|
from pygls.lsp.types import (
|
||||||
|
CompletionItem,
|
||||||
|
CompletionItemKind,
|
||||||
|
CompletionList,
|
||||||
|
CompletionOptions,
|
||||||
|
CompletionParams,
|
||||||
|
CompletionTriggerKind,
|
||||||
|
DocumentFormattingParams,
|
||||||
|
Hover,
|
||||||
|
InitializeParams,
|
||||||
|
MarkupContent,
|
||||||
|
MarkupKind,
|
||||||
|
Position,
|
||||||
|
Range,
|
||||||
|
TextDocumentPositionParams,
|
||||||
|
TextDocumentSaveRegistrationOptions,
|
||||||
|
TextEdit,
|
||||||
|
)
|
||||||
from pygls.server import LanguageServer
|
from pygls.server import LanguageServer
|
||||||
from pygls.types import (CompletionItem, CompletionItemKind, CompletionList,
|
|
||||||
CompletionParams, CompletionTriggerKind,
|
|
||||||
DocumentFormattingParams, Hover, InitializeParams,
|
|
||||||
MarkupContent, MarkupKind, Position, Range,
|
|
||||||
TextDocumentPositionParams, TextEdit)
|
|
||||||
|
|
||||||
from .api import API
|
from .api import API
|
||||||
from .formatter import Formatter
|
from .formatter import Formatter
|
||||||
@@ -21,118 +40,218 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class CMakeLanguageServer(LanguageServer):
|
class CMakeLanguageServer(LanguageServer):
|
||||||
_parser: ListParser
|
_parser: ListParser
|
||||||
_api: API
|
_api: Optional[API]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, *args: Any) -> None:
|
||||||
super().__init__()
|
super().__init__(*args)
|
||||||
|
|
||||||
self._parser = ListParser()
|
self._parser = ListParser()
|
||||||
self._api = None
|
self._api = None
|
||||||
|
|
||||||
@self.feature(INITIALIZE)
|
@self.feature(INITIALIZE)
|
||||||
def initialize(params: InitializeParams):
|
def initialize(params: InitializeParams) -> None:
|
||||||
opts = params.initializationOptions
|
opts = params.initialization_options
|
||||||
|
|
||||||
cmake = getattr(opts, 'cmakeExecutable', 'cmake')
|
cmake = getattr(opts, "cmakeExecutable", "cmake")
|
||||||
builddir = getattr(opts, 'buildDirectory', '')
|
builddir = getattr(opts, "buildDirectory", "")
|
||||||
logging.info(f'cmakeExecutable={cmake}, buildDirectory={builddir}')
|
logging.info(f"cmakeExecutable={cmake}, buildDirectory={builddir}")
|
||||||
|
|
||||||
self._api = API(cmake, Path(builddir))
|
self._api = API(cmake, Path(builddir))
|
||||||
self._api.parse_doc()
|
self._api.parse_doc()
|
||||||
|
|
||||||
@self.feature(COMPLETION, trigger_characters=['{'])
|
trigger_characters = ["{", "("]
|
||||||
def completions(params: CompletionParams):
|
|
||||||
if (params.context.triggerKind ==
|
@self.feature(
|
||||||
CompletionTriggerKind.TriggerCharacter):
|
COMPLETION, CompletionOptions(trigger_characters=trigger_characters)
|
||||||
token = ''
|
)
|
||||||
trigger = params.context.triggerCharacter
|
def completions(params: CompletionParams) -> CompletionList:
|
||||||
|
assert self._api is not None
|
||||||
|
|
||||||
|
if (
|
||||||
|
params.context is not None
|
||||||
|
and params.context.trigger_kind
|
||||||
|
== CompletionTriggerKind.TriggerCharacter
|
||||||
|
):
|
||||||
|
token = ""
|
||||||
|
trigger = params.context.trigger_character
|
||||||
else:
|
else:
|
||||||
ret = self.cursor_word(params.textDocument.uri,
|
line = self._cursor_line(params.text_document.uri, params.position)
|
||||||
params.position, False)
|
idx = params.position.character - 1
|
||||||
if not ret:
|
if 0 <= idx < len(line) and line[idx] in trigger_characters:
|
||||||
return None
|
token = ""
|
||||||
token = ret[0]
|
trigger = line[idx]
|
||||||
|
else:
|
||||||
|
word = self._cursor_word(
|
||||||
|
params.text_document.uri, params.position, False
|
||||||
|
)
|
||||||
|
token = "" if word is None else word[0]
|
||||||
trigger = None
|
trigger = None
|
||||||
|
|
||||||
items: List[CompletionItem] = []
|
items: List[CompletionItem] = []
|
||||||
|
|
||||||
if trigger != '{':
|
if trigger is None:
|
||||||
commands = self._api.search_command(token)
|
commands = self._api.search_command(token)
|
||||||
items.extend(
|
items.extend(
|
||||||
CompletionItem(x,
|
CompletionItem(
|
||||||
CompletionItemKind.Function,
|
label=x,
|
||||||
documentation=self._api.get_command_doc(x))
|
kind=CompletionItemKind.Function,
|
||||||
for x in commands)
|
documentation=self._api.get_command_doc(x),
|
||||||
|
insert_text=x,
|
||||||
|
)
|
||||||
|
for x in commands
|
||||||
|
)
|
||||||
|
|
||||||
|
if trigger is None or trigger == "{":
|
||||||
variables = self._api.search_variable(token)
|
variables = self._api.search_variable(token)
|
||||||
items.extend(
|
items.extend(
|
||||||
CompletionItem(x,
|
CompletionItem(
|
||||||
CompletionItemKind.Variable,
|
label=x,
|
||||||
documentation=self._api.get_variable_doc(x))
|
kind=CompletionItemKind.Variable,
|
||||||
for x in variables)
|
documentation=self._api.get_variable_doc(x),
|
||||||
|
insert_text=x,
|
||||||
|
)
|
||||||
|
for x in variables
|
||||||
|
)
|
||||||
|
|
||||||
if trigger != '{':
|
if trigger is None:
|
||||||
targets = self._api.search_target(token)
|
targets = self._api.search_target(token)
|
||||||
items.extend(
|
items.extend(
|
||||||
CompletionItem(x, CompletionItemKind.Class)
|
CompletionItem(
|
||||||
for x in targets)
|
lable=x, kind=CompletionItemKind.Class, insert_text=x
|
||||||
|
)
|
||||||
|
for x in targets
|
||||||
|
)
|
||||||
|
|
||||||
return CompletionList(False, items)
|
if trigger == "(":
|
||||||
|
func = self._cursor_function(params.text_document.uri, params.position)
|
||||||
|
if func is not None:
|
||||||
|
func = func.lower()
|
||||||
|
if func == "include":
|
||||||
|
modules = self._api.search_module(token, False)
|
||||||
|
items.extend(
|
||||||
|
CompletionItem(
|
||||||
|
label=x,
|
||||||
|
kind=CompletionItemKind.Module,
|
||||||
|
documentation=self._api.get_module_doc(x, False),
|
||||||
|
insert_text=x,
|
||||||
|
)
|
||||||
|
for x in modules
|
||||||
|
)
|
||||||
|
elif func == "find_package":
|
||||||
|
modules = self._api.search_module(token, True)
|
||||||
|
items.extend(
|
||||||
|
CompletionItem(
|
||||||
|
label=x,
|
||||||
|
kind=CompletionItemKind.Module,
|
||||||
|
documentation=self._api.get_module_doc(x, True),
|
||||||
|
insert_text=x,
|
||||||
|
)
|
||||||
|
for x in modules
|
||||||
|
)
|
||||||
|
|
||||||
|
return CompletionList(is_incomplete=False, items=items)
|
||||||
|
|
||||||
@self.feature(FORMATTING)
|
@self.feature(FORMATTING)
|
||||||
def formatting(params: DocumentFormattingParams):
|
def formatting(params: DocumentFormattingParams) -> Optional[List[TextEdit]]:
|
||||||
doc = self.workspace.get_document(params.textDocument.uri)
|
doc = self.workspace.get_document(params.text_document.uri)
|
||||||
content = doc.source
|
content = doc.source
|
||||||
tokens, remain = self._parser.parse(content)
|
tokens, remain = self._parser.parse(content)
|
||||||
if remain:
|
if remain:
|
||||||
self.show_message('CMake parser failed')
|
self.show_message("CMake parser failed")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
formatted = Formatter().format(tokens)
|
formatted = Formatter().format(tokens)
|
||||||
lines = content.count('\n')
|
lines = content.count("\n")
|
||||||
return [
|
return [
|
||||||
TextEdit(Range(Position(0, 0), Position(lines + 1, 0)),
|
TextEdit(
|
||||||
formatted)
|
range=Range(
|
||||||
|
start=Position(line=0, character=0),
|
||||||
|
end=Position(line=lines + 1, character=0),
|
||||||
|
),
|
||||||
|
new_text=formatted,
|
||||||
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
@self.feature(HOVER)
|
@self.feature(HOVER)
|
||||||
def hover(params: TextDocumentPositionParams):
|
def hover(params: TextDocumentPositionParams) -> Optional[Hover]:
|
||||||
ret = self.cursor_word(params.textDocument.uri, params.position)
|
assert self._api is not None
|
||||||
if not ret:
|
api = self._api
|
||||||
|
|
||||||
|
word = self._cursor_word(params.text_document.uri, params.position, True)
|
||||||
|
if not word:
|
||||||
return None
|
return None
|
||||||
doc = self._api.get_command_doc(ret[0].lower())
|
|
||||||
if not doc:
|
candidates: List[Callable[[str], Optional[str]]] = [
|
||||||
doc = self._api.get_variable_doc(ret[0])
|
lambda x: api.get_command_doc(x.lower()),
|
||||||
if not doc:
|
lambda x: api.get_variable_doc(x),
|
||||||
|
lambda x: api.get_module_doc(x, False),
|
||||||
|
lambda x: api.get_module_doc(x, True),
|
||||||
|
]
|
||||||
|
for c in candidates:
|
||||||
|
doc = c(word[0])
|
||||||
|
if doc is None:
|
||||||
|
continue
|
||||||
|
return Hover(
|
||||||
|
contents=MarkupContent(kind=MarkupKind.Markdown, value=doc),
|
||||||
|
range=word[1],
|
||||||
|
)
|
||||||
return None
|
return None
|
||||||
return Hover(MarkupContent(MarkupKind.Markdown, doc), ret[1])
|
|
||||||
|
|
||||||
@self.thread()
|
@self.thread()
|
||||||
@self.feature(TEXT_DOCUMENT_DID_SAVE, includeText=False)
|
@self.feature(
|
||||||
|
TEXT_DOCUMENT_DID_SAVE,
|
||||||
|
TextDocumentSaveRegistrationOptions(include_text=False),
|
||||||
|
)
|
||||||
@self.feature(INITIALIZED)
|
@self.feature(INITIALIZED)
|
||||||
def run_cmake(*args):
|
def run_cmake(*args: Any) -> None:
|
||||||
|
assert self._api is not None
|
||||||
|
|
||||||
if self._api.query():
|
if self._api.query():
|
||||||
self._api.read_reply()
|
self._api.read_reply()
|
||||||
|
|
||||||
def cursor_word(self,
|
def _cursor_function(self, uri: str, position: Position) -> Optional[str]:
|
||||||
uri: str,
|
doc = self.workspace.get_document(uri)
|
||||||
position: Position,
|
lines = doc.source.split("\n")[: position.line + 1]
|
||||||
include_all: bool = True) -> Optional[Tuple[str, Range]]:
|
lines[-1] = lines[-1][: position.character - 1].strip()
|
||||||
|
words = re.split(r"[\s\n()]+", "\n".join(lines))
|
||||||
|
return words[-1] if words else None
|
||||||
|
|
||||||
|
def _cursor_line(self, uri: str, position: Position) -> str:
|
||||||
doc = self.workspace.get_document(uri)
|
doc = self.workspace.get_document(uri)
|
||||||
content = doc.source
|
content = doc.source
|
||||||
line = content.split('\n')[position.line]
|
line = content.split("\n")[position.line]
|
||||||
cursor = position.character
|
return str(line)
|
||||||
for m in re.finditer(r'\w+', line):
|
|
||||||
if m.start() <= cursor <= m.end():
|
|
||||||
end = m.end() if include_all else cursor
|
|
||||||
return (line[m.start():end],
|
|
||||||
Range(Position(position.line, m.start()),
|
|
||||||
Position(position.line, end)))
|
|
||||||
|
|
||||||
|
def _cursor_word(
|
||||||
|
self, uri: str, position: Position, include_all: bool = True
|
||||||
|
) -> Optional[Tuple[str, Range]]:
|
||||||
|
line = self._cursor_line(uri, position)
|
||||||
|
cursor = position.character
|
||||||
|
for m in re.finditer(r"\w+", line):
|
||||||
|
end = m.end() if include_all else cursor
|
||||||
|
if m.start() <= cursor <= m.end():
|
||||||
|
word = (
|
||||||
|
line[m.start() : end],
|
||||||
|
Range(
|
||||||
|
start=Position(line=position.line, character=m.start()),
|
||||||
|
end=Position(line=position.line, character=end),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return word
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
from . import __version__
|
||||||
|
|
||||||
|
parser = ArgumentParser(description="CMake Language Server")
|
||||||
|
parser.add_argument(
|
||||||
|
"--version", action="version", version=f"%(prog)s {__version__}"
|
||||||
|
)
|
||||||
|
parser.parse_args()
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
logging.getLogger('pygls').setLevel(logging.WARNING)
|
logging.getLogger("pygls").setLevel(logging.WARNING)
|
||||||
CMakeLanguageServer().start_io()
|
CMakeLanguageServer().start_io() # type: ignore
|
||||||
|
|||||||
@@ -1,16 +1,62 @@
|
|||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import pprint
|
||||||
|
from pathlib import Path
|
||||||
|
from subprocess import PIPE, run
|
||||||
|
from threading import Thread
|
||||||
|
from typing import Iterable, Tuple
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from cmake_language_server.server import CMakeLanguageServer
|
||||||
|
from pygls.lsp.methods import EXIT
|
||||||
|
from pygls.server import LanguageServer
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def cmake_build(shared_datadir):
|
def cmake_build(shared_datadir: Path) -> Iterable[Path]:
|
||||||
from subprocess import run, PIPE
|
source = shared_datadir / "cmake"
|
||||||
source = shared_datadir / 'cmake'
|
build = source / "build"
|
||||||
build = source / 'build'
|
|
||||||
build.mkdir()
|
build.mkdir()
|
||||||
run(['cmake', source],
|
p = run(
|
||||||
check=True,
|
["cmake", str(source)],
|
||||||
cwd=build,
|
cwd=build,
|
||||||
stdout=PIPE,
|
stdout=PIPE,
|
||||||
stderr=PIPE,
|
stderr=PIPE,
|
||||||
universal_newlines=True)
|
universal_newlines=True,
|
||||||
|
)
|
||||||
|
if p.returncode != 0:
|
||||||
|
logging.error("env:\n" + pprint.pformat(os.environ))
|
||||||
|
logging.error("stdout:\n" + p.stdout)
|
||||||
|
logging.error("stderr:\n" + p.stderr)
|
||||||
|
raise RuntimeError("CMake failed")
|
||||||
yield build
|
yield build
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def client_server() -> Iterable[Tuple[LanguageServer, CMakeLanguageServer]]:
|
||||||
|
c2s_r, c2s_w = os.pipe()
|
||||||
|
s2c_r, s2c_w = os.pipe()
|
||||||
|
|
||||||
|
def start(ls: LanguageServer, fdr: int, fdw: int) -> None:
|
||||||
|
# TODO: better patch is needed
|
||||||
|
# disable `close()` to avoid error messages
|
||||||
|
close = ls.loop.close
|
||||||
|
ls.loop.close = lambda: None # type: ignore
|
||||||
|
ls.start_io(os.fdopen(fdr, "rb"), os.fdopen(fdw, "wb")) # type: ignore
|
||||||
|
ls.loop.close = close # type: ignore
|
||||||
|
|
||||||
|
server = CMakeLanguageServer(asyncio.new_event_loop())
|
||||||
|
server_thread = Thread(target=start, args=(server, c2s_r, s2c_w))
|
||||||
|
server_thread.start()
|
||||||
|
|
||||||
|
client = LanguageServer(asyncio.new_event_loop())
|
||||||
|
client_thread = Thread(target=start, args=(client, s2c_r, c2s_w))
|
||||||
|
client_thread.start()
|
||||||
|
|
||||||
|
yield client, server
|
||||||
|
|
||||||
|
client.send_notification(EXIT)
|
||||||
|
server.send_notification(EXIT)
|
||||||
|
server_thread.join()
|
||||||
|
client_thread.join()
|
||||||
|
|||||||
@@ -1,75 +1,119 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from cmake_language_server.api import API
|
from cmake_language_server.api import API
|
||||||
|
|
||||||
|
|
||||||
def test_query_with_cache(cmake_build):
|
def test_query_with_cache(cmake_build: Path) -> None:
|
||||||
api = API('cmake', cmake_build)
|
api = API("cmake", cmake_build)
|
||||||
assert api.query()
|
assert api.query()
|
||||||
|
|
||||||
query = cmake_build / '.cmake' / 'api' / 'v1' / 'query'
|
query = cmake_build / ".cmake" / "api" / "v1" / "query"
|
||||||
assert query.exists()
|
assert query.exists()
|
||||||
|
|
||||||
reply = cmake_build / '.cmake' / 'api' / 'v1' / 'reply'
|
reply = cmake_build / ".cmake" / "api" / "v1" / "reply"
|
||||||
assert reply.exists()
|
assert reply.exists()
|
||||||
|
|
||||||
|
|
||||||
def test_query_without_cache(cmake_build):
|
def test_query_without_cache(cmake_build: Path) -> None:
|
||||||
api = API('cmake', cmake_build)
|
api = API("cmake", cmake_build)
|
||||||
(cmake_build / 'CMakeCache.txt').unlink()
|
(cmake_build / "CMakeCache.txt").unlink()
|
||||||
|
|
||||||
assert not api.query()
|
assert not api.query()
|
||||||
|
|
||||||
|
|
||||||
def test_read_variable(cmake_build):
|
def test_read_variable(cmake_build: Path) -> None:
|
||||||
api = API('cmake', cmake_build)
|
api = API("cmake", cmake_build)
|
||||||
assert api.query()
|
assert api.query()
|
||||||
assert api.read_reply()
|
assert api.read_reply()
|
||||||
|
|
||||||
assert api.get_variable_doc('testproject_BINARY_DIR')
|
assert api.get_variable_doc("testproject_BINARY_DIR")
|
||||||
|
|
||||||
|
|
||||||
def test_read_cmake_files(cmake_build):
|
def test_read_cmake_files(cmake_build: Path) -> None:
|
||||||
api = API('cmake', cmake_build)
|
api = API("cmake", cmake_build)
|
||||||
api.parse_doc()
|
api.parse_doc()
|
||||||
assert api.query()
|
assert api.query()
|
||||||
api.read_reply()
|
api.read_reply()
|
||||||
|
|
||||||
assert 'GNU' in api.get_variable_doc('CMAKE_CXX_COMPILER_ID')
|
import platform
|
||||||
|
|
||||||
|
system = platform.system()
|
||||||
|
if system == "Linux":
|
||||||
|
assert "GNU" in api.get_variable_doc("CMAKE_CXX_COMPILER_ID")
|
||||||
|
elif system == "Windows":
|
||||||
|
assert "MSVC" in api.get_variable_doc("CMAKE_CXX_COMPILER_ID")
|
||||||
|
elif system == "Darwin":
|
||||||
|
assert "Clang" in api.get_variable_doc("CMAKE_CXX_COMPILER_ID")
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Unexpected system")
|
||||||
|
|
||||||
|
|
||||||
def test_parse_commands(cmake_build):
|
def test_parse_commands(cmake_build: Path) -> None:
|
||||||
api = API('cmake', cmake_build)
|
api = API("cmake", cmake_build)
|
||||||
api.parse_doc()
|
api.parse_doc()
|
||||||
|
|
||||||
p = subprocess.run(['cmake', '--help-command-list'],
|
p = subprocess.run(
|
||||||
|
["cmake", "--help-command-list"],
|
||||||
universal_newlines=True,
|
universal_newlines=True,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE)
|
stderr=subprocess.PIPE,
|
||||||
commands = p.stdout.strip().split('\n')
|
)
|
||||||
|
commands = p.stdout.strip().split("\n")
|
||||||
|
|
||||||
for command in commands:
|
for command in commands:
|
||||||
assert api.get_command_doc(command) is not None, f'{command} not found'
|
assert api.get_command_doc(command) is not None, f"{command} not found"
|
||||||
|
|
||||||
assert 'break()' in api.get_command_doc('break')
|
assert "break()" in api.get_command_doc("break")
|
||||||
assert api.get_command_doc('not_existing_command') is None
|
assert api.get_command_doc("not_existing_command") is None
|
||||||
|
|
||||||
|
|
||||||
def test_parse_variables(cmake_build):
|
def test_parse_variables(cmake_build: Path) -> None:
|
||||||
api = API('cmake', cmake_build)
|
api = API("cmake", cmake_build)
|
||||||
api.parse_doc()
|
api.parse_doc()
|
||||||
|
|
||||||
p = subprocess.run(['cmake', '--help-variable-list'],
|
p = subprocess.run(
|
||||||
|
["cmake", "--help-variable-list"],
|
||||||
universal_newlines=True,
|
universal_newlines=True,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE)
|
stderr=subprocess.PIPE,
|
||||||
variables = p.stdout.strip().split('\n')
|
)
|
||||||
|
variables = p.stdout.strip().split("\n")
|
||||||
|
|
||||||
for variable in variables:
|
for variable in variables:
|
||||||
if '<' in variable:
|
if "<" in variable:
|
||||||
continue
|
continue
|
||||||
assert api.get_variable_doc(
|
assert api.get_variable_doc(variable) is not None, f"{variable} not found"
|
||||||
variable) is not None, f'{variable} not found'
|
|
||||||
|
|
||||||
assert api.get_variable_doc('BUILD_SHARED_LIBS') is not None
|
assert api.get_variable_doc("BUILD_SHARED_LIBS") is not None
|
||||||
assert api.get_variable_doc('not_existing_variable') is None
|
assert api.get_variable_doc("not_existing_variable") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_modules(cmake_build: Path) -> None:
|
||||||
|
api = API("cmake", cmake_build)
|
||||||
|
api.parse_doc()
|
||||||
|
|
||||||
|
p = subprocess.run(
|
||||||
|
["cmake", "--help-module-list"],
|
||||||
|
universal_newlines=True,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
)
|
||||||
|
modules = p.stdout.strip().split("\n")
|
||||||
|
|
||||||
|
for module in modules:
|
||||||
|
if module.startswith("Find"):
|
||||||
|
assert (
|
||||||
|
api.get_module_doc(module[4:], True) is not None
|
||||||
|
), f"{module} not found"
|
||||||
|
else:
|
||||||
|
assert api.get_module_doc(module, False) is not None, f"{module} not found"
|
||||||
|
|
||||||
|
assert api.get_module_doc("GoogleTest", False) is not None
|
||||||
|
assert api.get_module_doc("GoogleTest", True) is None
|
||||||
|
assert api.search_module("GoogleTest", False) == ["GoogleTest"]
|
||||||
|
assert api.search_module("GoogleTest", True) == []
|
||||||
|
assert api.get_module_doc("Boost", False) is None
|
||||||
|
assert api.get_module_doc("Boost", True) is not None
|
||||||
|
assert api.search_module("Boost", False) == []
|
||||||
|
assert api.search_module("Boost", True) == ["Boost"]
|
||||||
|
|||||||
@@ -1,9 +1,16 @@
|
|||||||
from cmake_language_server.formatter import Formatter
|
import sys
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from io import StringIO
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Callable, Iterator
|
||||||
|
|
||||||
|
from cmake_language_server.formatter import Formatter, main
|
||||||
from cmake_language_server.parser import ListParser
|
from cmake_language_server.parser import ListParser
|
||||||
|
from pytest import CaptureFixture
|
||||||
|
|
||||||
|
|
||||||
def make_formatter_test(liststr: str, expect: str):
|
def make_formatter_test(liststr: str, expect: str) -> Callable[[], None]:
|
||||||
def test():
|
def test() -> None:
|
||||||
tokens, remain = ListParser().parse(liststr)
|
tokens, remain = ListParser().parse(liststr)
|
||||||
actual = Formatter().format(tokens)
|
actual = Formatter().format(tokens)
|
||||||
assert actual == expect
|
assert actual == expect
|
||||||
@@ -11,50 +18,57 @@ def make_formatter_test(liststr: str, expect: str):
|
|||||||
return test
|
return test
|
||||||
|
|
||||||
|
|
||||||
test_command = make_formatter_test('a()', 'a()\n')
|
test_command = make_formatter_test("a()", "a()\n")
|
||||||
test_command_tolower = make_formatter_test('A()', 'a()\n')
|
test_command_tolower = make_formatter_test("A()", "a()\n")
|
||||||
test_remove_space = make_formatter_test('''
|
test_remove_space = make_formatter_test(
|
||||||
|
"""
|
||||||
#a
|
#a
|
||||||
b ( c ) # d
|
b ( c ) # d
|
||||||
''', '''\
|
""",
|
||||||
|
"""\
|
||||||
#a
|
#a
|
||||||
b(c) # d
|
b(c) # d
|
||||||
''')
|
""",
|
||||||
|
)
|
||||||
test_indent_if = make_formatter_test(
|
test_indent_if = make_formatter_test(
|
||||||
'''
|
"""
|
||||||
if()
|
if()
|
||||||
a() # a
|
a() # a
|
||||||
else()
|
else()
|
||||||
# b
|
# b
|
||||||
b()
|
b()
|
||||||
endif()
|
endif()
|
||||||
''', '''\
|
""",
|
||||||
|
"""\
|
||||||
if()
|
if()
|
||||||
a() # a
|
a() # a
|
||||||
else()
|
else()
|
||||||
# b
|
# b
|
||||||
b()
|
b()
|
||||||
endif()
|
endif()
|
||||||
''')
|
""",
|
||||||
|
)
|
||||||
test_indent_if_nested = make_formatter_test(
|
test_indent_if_nested = make_formatter_test(
|
||||||
'''
|
"""
|
||||||
if()
|
if()
|
||||||
if()
|
if()
|
||||||
a()
|
a()
|
||||||
b()
|
b()
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
''', '''\
|
""",
|
||||||
|
"""\
|
||||||
if()
|
if()
|
||||||
if()
|
if()
|
||||||
a()
|
a()
|
||||||
b()
|
b()
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
''')
|
""",
|
||||||
test_argument = make_formatter_test('a( b c d)', 'a(b c d)\n')
|
)
|
||||||
|
test_argument = make_formatter_test("a( b c d)", "a(b c d)\n")
|
||||||
test_argument_multiline = make_formatter_test(
|
test_argument_multiline = make_formatter_test(
|
||||||
'''
|
"""
|
||||||
if()
|
if()
|
||||||
a(b c
|
a(b c
|
||||||
d # e
|
d # e
|
||||||
@@ -62,7 +76,8 @@ f
|
|||||||
# g
|
# g
|
||||||
) # h
|
) # h
|
||||||
endif()
|
endif()
|
||||||
''', '''\
|
""",
|
||||||
|
"""\
|
||||||
if()
|
if()
|
||||||
a(
|
a(
|
||||||
b c
|
b c
|
||||||
@@ -71,4 +86,83 @@ if()
|
|||||||
# g
|
# g
|
||||||
) # h
|
) # h
|
||||||
endif()
|
endif()
|
||||||
''')
|
""",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def mock_stdin(buf: str) -> Iterator[None]:
|
||||||
|
stdin = sys.stdin
|
||||||
|
sys.stdin = StringIO(buf)
|
||||||
|
yield
|
||||||
|
sys.stdin = stdin
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_stdin(capsys: CaptureFixture[str]) -> None:
|
||||||
|
with mock_stdin(" a()"):
|
||||||
|
main([])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert captured.out == "a()\n"
|
||||||
|
assert captured.err == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_stdin_diff(capsys: CaptureFixture[str]) -> None:
|
||||||
|
with mock_stdin(" a()"):
|
||||||
|
main(["-d"])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert "- a()" in captured.out
|
||||||
|
assert "+a()" in captured.out
|
||||||
|
assert captured.err == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_file_1(capsys: CaptureFixture[str], tmp_path: Path) -> None:
|
||||||
|
testfile1 = tmp_path / "list1.cmake"
|
||||||
|
with testfile1.open("w") as fp:
|
||||||
|
fp.write(" a()")
|
||||||
|
|
||||||
|
main([str(testfile1)])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert captured.out == "a()\n"
|
||||||
|
assert captured.err == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_file_2(capsys: CaptureFixture[str], tmp_path: Path) -> None:
|
||||||
|
testfile1 = tmp_path / "list1.cmake"
|
||||||
|
with testfile1.open("w") as fp:
|
||||||
|
fp.write(" a()")
|
||||||
|
testfile2 = tmp_path / "list2.cmake"
|
||||||
|
with testfile2.open("w") as fp:
|
||||||
|
fp.write(" b()")
|
||||||
|
|
||||||
|
main([str(testfile1), str(testfile2)])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert captured.out == "a()\nb()\n"
|
||||||
|
assert captured.err == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_inplace(capsys: CaptureFixture[str], tmp_path: Path) -> None:
|
||||||
|
testfile1 = tmp_path / "list1.cmake"
|
||||||
|
with testfile1.open("w") as fp:
|
||||||
|
fp.write(" a()")
|
||||||
|
|
||||||
|
main(["-i", str(testfile1)])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert captured.out == ""
|
||||||
|
assert captured.err == ""
|
||||||
|
|
||||||
|
with testfile1.open() as fp:
|
||||||
|
content = fp.read()
|
||||||
|
assert content == "a()\n"
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_diff(capsys: CaptureFixture[str], tmp_path: Path) -> None:
|
||||||
|
testfile1 = tmp_path / "list1.cmake"
|
||||||
|
with testfile1.open("w") as fp:
|
||||||
|
fp.write(" a()")
|
||||||
|
|
||||||
|
main(["-d", str(testfile1)])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert str(testfile1) in captured.out
|
||||||
|
assert "- a()" in captured.out
|
||||||
|
assert "+a()" in captured.out
|
||||||
|
assert captured.err == ""
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
from typing import List
|
from typing import Callable, List
|
||||||
|
|
||||||
from cmake_language_server.parser import ListParser, TokenType
|
from cmake_language_server.parser import ListParser, TokenType
|
||||||
|
|
||||||
|
|
||||||
def make_parser_test(liststr: str,
|
def make_parser_test(
|
||||||
expect_token: List[TokenType],
|
liststr: str, expect_token: List[TokenType], expect_remain: str = ""
|
||||||
expect_remain: str = ''):
|
) -> Callable[[], None]:
|
||||||
def test():
|
def test() -> None:
|
||||||
actual_token, actual_remain = ListParser().parse(liststr)
|
actual_token, actual_remain = ListParser().parse(liststr)
|
||||||
assert actual_token == expect_token
|
assert actual_token == expect_token
|
||||||
assert actual_remain == expect_remain
|
assert actual_remain == expect_remain
|
||||||
@@ -14,51 +14,70 @@ def make_parser_test(liststr: str,
|
|||||||
return test
|
return test
|
||||||
|
|
||||||
|
|
||||||
test_command_no_args = make_parser_test('a()', [('a', [])])
|
test_command_no_args = make_parser_test("a()", [("a", [])])
|
||||||
test_command_space = make_parser_test(' a ()', [' ', ('a', [])])
|
test_command_space = make_parser_test(" a ()", [" ", ("a", [])])
|
||||||
test_command_arg = make_parser_test('a(b)', [('a', ['b'])])
|
test_command_arg = make_parser_test("a(b)", [("a", ["b"])])
|
||||||
test_command_arg_space = make_parser_test('a ( b )', [('a', ['b'])])
|
test_command_arg_space = make_parser_test("a ( b )", [("a", ["b"])])
|
||||||
test_command_arg_escape = make_parser_test(r'a(\n\")', [('a', [r'\n\"'])])
|
test_command_arg_escape = make_parser_test(r"a(\n\")", [("a", [r"\n\""])])
|
||||||
test_command_arg_paren = make_parser_test('a((b))', [('a', ['(', 'b', ')'])])
|
test_command_arg_paren = make_parser_test("a((b))", [("a", ["(", "b", ")"])])
|
||||||
test_command_arg_paren_paren = make_parser_test(
|
test_command_arg_paren_paren = make_parser_test(
|
||||||
'a(((b)))', [('a', ['(', '(', 'b', ')', ')'])])
|
"a(((b)))", [("a", ["(", "(", "b", ")", ")"])]
|
||||||
test_command_arg_quote = make_parser_test(r'a("b\"")', [('a', [r'"b\""'])])
|
)
|
||||||
test_command_arg_quote_cont = make_parser_test('a("\\\n")',
|
test_command_arg_quote = make_parser_test(r'a("b\"")', [("a", [r'"b\""'])])
|
||||||
[('a', ['"\\\n"'])])
|
test_command_arg_quote_cont = make_parser_test('a("\\\n")', [("a", ['"\\\n"'])])
|
||||||
test_command_arg_quo_multiline = make_parser_test('''a("b
|
test_command_arg_quo_multiline = make_parser_test(
|
||||||
|
"""a("b
|
||||||
c
|
c
|
||||||
")''', [('a', ['"b\nc\n"'])])
|
")""",
|
||||||
test_command_arg_bracket_0 = make_parser_test('a([[b]])', [('a', ['[[b]]'])])
|
[("a", ['"b\nc\n"'])],
|
||||||
test_command_arg_bracket_1 = make_parser_test('a([=[b]=])',
|
)
|
||||||
[('a', ['[=[b]=]'])])
|
test_command_arg_bracket_0 = make_parser_test("a([[b]])", [("a", ["[[b]]"])])
|
||||||
test_command_arg_space = make_parser_test('a ( b )', [('a', [' ', 'b', ' '])])
|
test_command_arg_bracket_1 = make_parser_test("a([=[b]=])", [("a", ["[=[b]=]"])])
|
||||||
test_command_arg_multi = make_parser_test('a(b c)', [('a', ['b', ' ', 'c'])])
|
test_command_arg_space = make_parser_test("a ( b )", [("a", [" ", "b", " "])])
|
||||||
test_command_multielement = make_parser_test('''a(
|
test_command_arg_multi = make_parser_test("a(b c)", [("a", ["b", " ", "c"])])
|
||||||
|
test_command_multielement = make_parser_test(
|
||||||
|
"""a(
|
||||||
b
|
b
|
||||||
c # c
|
c # c
|
||||||
)''', [('a', ['\n', ' ', 'b', '\n', ' ', 'c', ' ', '# c', '\n'])])
|
)""",
|
||||||
test_line_comment = make_parser_test('a() # b # c',
|
[("a", ["\n", " ", "b", "\n", " ", "c", " ", "# c", "\n"])],
|
||||||
[('a', []), ' ', '# b # c'])
|
)
|
||||||
test_bracket_comment = make_parser_test('#[[a]]#[[b]]', ['#[[a]]', '#[[b]]'])
|
test_line_comment = make_parser_test("a() # b # c", [("a", []), " ", "# b # c"])
|
||||||
test_bracket_comment_nested = make_parser_test('#[=[[[a]]]=]',
|
test_bracket_comment = make_parser_test("#[[a]]#[[b]]", ["#[[a]]", "#[[b]]"])
|
||||||
['#[=[[[a]]]=]'])
|
test_bracket_comment_nested = make_parser_test("#[=[[[a]]]=]", ["#[=[[[a]]]=]"])
|
||||||
test_bracket_comment_multiline = make_parser_test('#[[\na\nb\nc\n]]',
|
test_bracket_comment_multiline = make_parser_test(
|
||||||
['#[[\na\nb\nc\n]]'])
|
"#[[\na\nb\nc\n]]", ["#[[\na\nb\nc\n]]"]
|
||||||
test_if_block = make_parser_test('''if()
|
)
|
||||||
|
test_if_block = make_parser_test(
|
||||||
|
"""if()
|
||||||
a()
|
a()
|
||||||
else()
|
else()
|
||||||
b()
|
b()
|
||||||
endif()''', [('if', []), '\n', ' ', ('a', []), '\n', ('else', []), '\n', ' ',
|
endif()""",
|
||||||
('b', []), '\n', ('endif', [])])
|
[
|
||||||
|
("if", []),
|
||||||
|
"\n",
|
||||||
|
" ",
|
||||||
|
("a", []),
|
||||||
|
"\n",
|
||||||
|
("else", []),
|
||||||
|
"\n",
|
||||||
|
" ",
|
||||||
|
("b", []),
|
||||||
|
"\n",
|
||||||
|
("endif", []),
|
||||||
|
],
|
||||||
|
)
|
||||||
test_comment_multi_linecomment = make_parser_test(
|
test_comment_multi_linecomment = make_parser_test(
|
||||||
'''a()# a
|
"""a()# a
|
||||||
b() # b
|
b() # b
|
||||||
c() # c''', [('a', []), '# a', '\n', ('b', []), ' ', '# b', '\n',
|
c() # c""",
|
||||||
('c', []), ' ', '# c'])
|
[("a", []), "# a", "\n", ("b", []), " ", "# b", "\n", ("c", []), " ", "# c"],
|
||||||
|
)
|
||||||
|
|
||||||
test_incomplete_id = make_parser_test('a', [], 'a')
|
test_incomplete_id = make_parser_test("a", [], "a")
|
||||||
test_incomplete_command = make_parser_test('a(', [], 'a(')
|
test_incomplete_command = make_parser_test("a(", [], "a(")
|
||||||
test_incomplete_id_after_command = make_parser_test('a()\nb',
|
test_incomplete_id_after_command = make_parser_test("a()\nb", [("a", []), "\n"], "b")
|
||||||
[('a', []), '\n'], 'b')
|
|
||||||
test_incomplete_command_after_command = make_parser_test(
|
test_incomplete_command_after_command = make_parser_test(
|
||||||
'a()\nb(c', [('a', []), '\n'], 'b(c')
|
"a()\nb(c", [("a", []), "\n"], "b(c"
|
||||||
|
)
|
||||||
|
|||||||
205
tests/test_server.py
Normal file
205
tests/test_server.py
Normal file
@@ -0,0 +1,205 @@
|
|||||||
|
from concurrent import futures
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict, Optional, Tuple
|
||||||
|
|
||||||
|
from cmake_language_server.server import CMakeLanguageServer
|
||||||
|
from pygls.lsp.methods import (
|
||||||
|
COMPLETION,
|
||||||
|
FORMATTING,
|
||||||
|
HOVER,
|
||||||
|
INITIALIZE,
|
||||||
|
TEXT_DOCUMENT_DID_OPEN,
|
||||||
|
)
|
||||||
|
from pygls.lsp.types import (
|
||||||
|
ClientCapabilities,
|
||||||
|
CompletionContext,
|
||||||
|
CompletionParams,
|
||||||
|
CompletionTriggerKind,
|
||||||
|
DidOpenTextDocumentParams,
|
||||||
|
DocumentFormattingParams,
|
||||||
|
FormattingOptions,
|
||||||
|
InitializeParams,
|
||||||
|
Position,
|
||||||
|
TextDocumentIdentifier,
|
||||||
|
TextDocumentItem,
|
||||||
|
TextDocumentPositionParams,
|
||||||
|
)
|
||||||
|
from pygls.server import LanguageServer
|
||||||
|
|
||||||
|
CALL_TIMEOUT = 2
|
||||||
|
|
||||||
|
|
||||||
|
def _init(client: LanguageServer, root: Path) -> None:
|
||||||
|
retry = 3
|
||||||
|
while retry > 0:
|
||||||
|
try:
|
||||||
|
client.lsp.send_request(
|
||||||
|
INITIALIZE,
|
||||||
|
InitializeParams(
|
||||||
|
process_id=1234,
|
||||||
|
root_uri=root.as_uri(),
|
||||||
|
capabilities=ClientCapabilities(),
|
||||||
|
),
|
||||||
|
).result(timeout=CALL_TIMEOUT)
|
||||||
|
except futures.TimeoutError:
|
||||||
|
retry -= 1
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
def _open(client: LanguageServer, path: Path, text: Optional[str] = None) -> None:
|
||||||
|
if text is None:
|
||||||
|
with open(path) as fp:
|
||||||
|
text = fp.read()
|
||||||
|
|
||||||
|
client.lsp.notify(
|
||||||
|
TEXT_DOCUMENT_DID_OPEN,
|
||||||
|
DidOpenTextDocumentParams(
|
||||||
|
text_document=TextDocumentItem(
|
||||||
|
uri=path.as_uri(), language_id="cmake", version=1, text=text
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _test_completion(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer],
|
||||||
|
datadir: Path,
|
||||||
|
content: str,
|
||||||
|
context: Optional[CompletionContext],
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
client, server = client_server
|
||||||
|
_init(client, datadir)
|
||||||
|
path = datadir / "CMakeLists.txt"
|
||||||
|
_open(client, path, content)
|
||||||
|
params = CompletionParams(
|
||||||
|
text_document=TextDocumentIdentifier(uri=path.as_uri()),
|
||||||
|
position=Position(line=0, character=len(content)),
|
||||||
|
context=context,
|
||||||
|
)
|
||||||
|
if context is None:
|
||||||
|
# some clients do not send context
|
||||||
|
del params.context
|
||||||
|
ret = client.lsp.send_request(COMPLETION, params).result(timeout=CALL_TIMEOUT)
|
||||||
|
assert isinstance(ret, dict)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def test_initialize(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
|
||||||
|
) -> None:
|
||||||
|
client, server = client_server
|
||||||
|
|
||||||
|
assert server._api is None
|
||||||
|
_init(client, datadir)
|
||||||
|
assert server._api is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_completions_invoked(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
|
||||||
|
) -> None:
|
||||||
|
response = _test_completion(
|
||||||
|
client_server,
|
||||||
|
datadir,
|
||||||
|
"projec",
|
||||||
|
CompletionContext(trigger_kind=CompletionTriggerKind.Invoked),
|
||||||
|
)
|
||||||
|
item = next(filter(lambda x: x["label"] == "project", response["items"]), None)
|
||||||
|
assert item is not None
|
||||||
|
assert isinstance(item["documentation"], str)
|
||||||
|
assert "<PROJECT-NAME>" in item["documentation"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_completions_nocontext(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
|
||||||
|
) -> None:
|
||||||
|
response = _test_completion(client_server, datadir, "projec", None)
|
||||||
|
item = next(filter(lambda x: x["label"] == "project", response["items"]), None)
|
||||||
|
assert item is not None
|
||||||
|
assert isinstance(item["documentation"], str)
|
||||||
|
assert "<PROJECT-NAME>" in item["documentation"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_completions_triggercharacter_variable(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
|
||||||
|
) -> None:
|
||||||
|
response = _test_completion(
|
||||||
|
client_server,
|
||||||
|
datadir,
|
||||||
|
"${",
|
||||||
|
CompletionContext(
|
||||||
|
trigger_kind=CompletionTriggerKind.TriggerCharacter, trigger_character="{"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
assert "PROJECT_VERSION" in [x["label"] for x in response["items"]]
|
||||||
|
|
||||||
|
response_nocontext = _test_completion(client_server, datadir, "${", None)
|
||||||
|
assert response == response_nocontext
|
||||||
|
|
||||||
|
|
||||||
|
def test_completions_triggercharacter_module(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
|
||||||
|
) -> None:
|
||||||
|
response = _test_completion(
|
||||||
|
client_server,
|
||||||
|
datadir,
|
||||||
|
"include(",
|
||||||
|
CompletionContext(
|
||||||
|
trigger_kind=CompletionTriggerKind.TriggerCharacter, trigger_character="("
|
||||||
|
),
|
||||||
|
)
|
||||||
|
assert "GoogleTest" in [x["label"] for x in response["items"]]
|
||||||
|
|
||||||
|
response_nocontext = _test_completion(client_server, datadir, "include(", None)
|
||||||
|
assert response == response_nocontext
|
||||||
|
|
||||||
|
|
||||||
|
def test_completions_triggercharacter_package(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
|
||||||
|
) -> None:
|
||||||
|
response = _test_completion(
|
||||||
|
client_server,
|
||||||
|
datadir,
|
||||||
|
"find_package(",
|
||||||
|
CompletionContext(
|
||||||
|
trigger_kind=CompletionTriggerKind.TriggerCharacter, trigger_character="("
|
||||||
|
),
|
||||||
|
)
|
||||||
|
assert "Boost" in [x["label"] for x in response["items"]]
|
||||||
|
|
||||||
|
response_nocontext = _test_completion(client_server, datadir, "find_package(", None)
|
||||||
|
assert response == response_nocontext
|
||||||
|
|
||||||
|
|
||||||
|
def test_formatting(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
|
||||||
|
) -> None:
|
||||||
|
client, server = client_server
|
||||||
|
_init(client, datadir)
|
||||||
|
path = datadir / "CMakeLists.txt"
|
||||||
|
_open(client, path, "a ( b c ) ")
|
||||||
|
response = client.lsp.send_request(
|
||||||
|
FORMATTING,
|
||||||
|
DocumentFormattingParams(
|
||||||
|
text_document=TextDocumentIdentifier(uri=path.as_uri()),
|
||||||
|
options=FormattingOptions(tab_size=2, insert_spaces=True),
|
||||||
|
),
|
||||||
|
).result(timeout=CALL_TIMEOUT)
|
||||||
|
assert response[0]["newText"] == "a(b c)\n"
|
||||||
|
|
||||||
|
|
||||||
|
def test_hover(
|
||||||
|
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
|
||||||
|
) -> None:
|
||||||
|
client, server = client_server
|
||||||
|
_init(client, datadir)
|
||||||
|
path = datadir / "CMakeLists.txt"
|
||||||
|
_open(client, path, "project()")
|
||||||
|
response = client.lsp.send_request(
|
||||||
|
HOVER,
|
||||||
|
TextDocumentPositionParams(
|
||||||
|
text_document=TextDocumentIdentifier(uri=path.as_uri()),
|
||||||
|
position=Position(line=0, character=0),
|
||||||
|
),
|
||||||
|
).result(timeout=CALL_TIMEOUT)
|
||||||
|
assert "<PROJECT-NAME>" in response["contents"]["value"]
|
||||||
23
tox.ini
23
tox.ini
@@ -1,27 +1,28 @@
|
|||||||
[tox]
|
[tox]
|
||||||
isolated_build = True
|
isolated_build = True
|
||||||
skipsdist = True
|
skipsdist = True
|
||||||
envlist = py36, py37, py38, lint
|
envlist = py36, py37, py38, py39, lint
|
||||||
|
|
||||||
[gh-actions]
|
[gh-actions]
|
||||||
python =
|
python =
|
||||||
3.6: py36
|
3.6: py36
|
||||||
3.7: py37, lint
|
3.7: py37
|
||||||
3.8: py38
|
3.8: py38, lint
|
||||||
|
3.9: py39
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
whitelist_externals = poetry
|
allowlist_externals =
|
||||||
|
poetry
|
||||||
|
git
|
||||||
skip_install = true
|
skip_install = true
|
||||||
|
passenv = INCLUDE LIB LIBPATH Platform VCTools* VSCMD_* WindowsSDK*
|
||||||
commands_pre =
|
commands_pre =
|
||||||
poetry install
|
poetry install
|
||||||
commands =
|
commands =
|
||||||
poetry run pytest -sv tests
|
poetry run pytest --cov-report=term --cov-report=xml --cov=src -sv tests
|
||||||
|
|
||||||
[testenv:lint]
|
[testenv:lint]
|
||||||
whitelist_externals = poetry
|
|
||||||
skip_install = true
|
|
||||||
commands =
|
commands =
|
||||||
poetry run isort -c -rc src tests
|
poetry run pysen run format
|
||||||
poetry run yapf -d -r src tests
|
git diff --exit-code --ignore-submodules
|
||||||
poetry run flake8
|
poetry run pysen run lint
|
||||||
poetry run mypy src tests
|
|
||||||
|
|||||||
Reference in New Issue
Block a user