16 Commits

Author SHA1 Message Date
Charlie Vieth
9630b96935 poetry: update pygls to version 0.11.2 (#42)
Update pygls to version 0.11.2 and change the version requirement to
'^0.11' this removes a conflict with jedi-language-server which requires
pygls>=0.11.1,<0.12.0.

This commit also bumps the cmake-language-server version to 0.1.3.
2021-10-03 15:13:39 +09:00
Regen
76e34ae628 Fix __version__ (#43) 2021-10-03 12:51:16 +09:00
Regen
cbb6bdd1ae Fix parse_modules (#44) 2021-10-03 12:46:02 +09:00
Regen
a5af5b505f Update pygls (#38) 2021-03-28 23:49:27 +09:00
Regen
4d120a6a98 Replace linter (#37) 2021-03-28 22:23:57 +09:00
KOLANICH
6e839f7675 Added .editorconfig. (#35)
Applies some elements of PEP8 during editing in the editors supporting it.
2021-01-23 16:07:22 +09:00
KOLANICH
cade1e2c45 Fixed build system requirements in pyproject.toml. (#34)
Poetry depends on lot of dependencies having nothing common with building packages. That's why poetry-core was created, which has much less dependencies and more focused on building wheels.
2021-01-23 16:04:06 +09:00
Regen
d16d3b24ef Merge pull request #31 from jargonzombies/doc-and-spelling
Some documentation and misspelling fixes
2020-11-08 16:48:02 +09:00
Jan-Grimo Sobez
4be7657edb Some documentation and misspelling fixes 2020-11-07 22:09:42 +01:00
Regen
040f0b9f0c Merge pull request #28 from regen100/add-doc
Add doc
2020-08-22 16:58:58 +09:00
Regen
ef2c31c6a3 Add doc 2020-08-22 15:15:45 +09:00
Regen
87879ee5df Merge pull request #27 from regen100/replace-linter
Replace yapf with black
2020-08-22 14:54:15 +09:00
Regen
01b1fac73e Replace yapf with black 2020-08-22 14:38:29 +09:00
Regen
5550cb259c Merge pull request #24 from r-burns/darwin
Fix test_read_cmake_files on macOS
2020-07-29 12:42:32 +09:00
Regen
466c5b7bcc Run CI on PR 2020-07-29 12:31:44 +09:00
Ryan Burns
0ec120f391 Fix test_read_cmake_files on macOS 2020-07-27 00:55:16 -07:00
20 changed files with 1494 additions and 903 deletions

12
.editorconfig Normal file
View 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
View File

@@ -0,0 +1 @@
poetry.lock linguist-generated=true

View File

@@ -1,13 +1,13 @@
name: Tests
on: [push]
on: [pull_request]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python: [3.6, 3.7, 3.8]
python: [3.6, 3.7, 3.8, 3.9]
os: [ubuntu-18.04, windows-2016]
steps:
- uses: actions/checkout@v2

129
.gitignore vendored
View File

@@ -1,133 +1,4 @@
### https://raw.github.com/github/gitignore/cb0c6ef7ac68f2300409ee85501d9ad432cb4c7e/Python.gitignore
# Byte-compiled / optimized / DLL files
__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/
.nox/
.coverage
.coverage.*
.cache
nosetests.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/

View File

@@ -1,2 +0,0 @@
[style]
based_on_style = pep8

View File

@@ -64,5 +64,13 @@ if executable('cmake-language-server')
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
[vim-lsp]: https://github.com/prabirshrestha/vim-lsp

View File

@@ -1,6 +0,0 @@
[mypy]
ignore_missing_imports = True
allow_redefinition = True
[mypy-re.Scanner]
ignore_errors = True

886
poetry.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "cmake-language-server"
version = "0.1.2"
version = "0.1.3"
description = "CMake LSP Implementation"
license = "MIT"
authors = ["regen"]
@@ -19,23 +19,35 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.6"
pygls = "^0.8.1"
pygls = "^0.11"
pyparsing = "^2.4"
importlib-metadata = {version = "^4.8", python = "<3.8"}
[tool.poetry.dev-dependencies]
flake8 = "^3.7"
mypy = "^0.740.0"
pytest = "^5.2"
yapf = "^0.28.0"
pytest = "^6.2"
pytest-datadir = "^1.3"
tox = "^3.14"
isort = "^4.3"
pytest-cov = "^2.8"
pytest-cov = "^2.11"
pysen = {version = "^0.9", extras = ["lint"]}
[tool.poetry.scripts]
cmake-format = "cmake_language_server.formatter:main"
cmake-language-server = "cmake_language_server.server:main"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
requires = ["poetry-core>=1.0.0"]
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 = ["."]

View File

@@ -1 +1,6 @@
__version__ = '0.1.2'
try:
import importlib.metadata as importlib_metadata
except ModuleNotFoundError:
import importlib_metadata
__version__ = importlib_metadata.version(__name__)

View File

@@ -12,10 +12,10 @@ 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('. ', '. ')
doc = re.sub(r":.+?:`(.+?)`", r"\1", doc)
doc = re.sub(r"``([^`]+)``", r"`\1`", doc)
doc = doc.replace("\n", " ")
doc = doc.replace(". ", ". ")
return doc
@@ -25,7 +25,7 @@ class API(object):
_uuid: uuid.UUID
_builtin_commands: 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]
_cached_variables: Dict[str, str]
@@ -45,82 +45,97 @@ class API(object):
self._generated_list_parsed = False
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():
return False
self.query_json.parent.mkdir(parents=True, exist_ok=True)
with self.query_json.open('w') as fp:
fp.write('''\
with self.query_json.open("w") as fp:
fp.write(
"""\
{
"requests": [
{"kind": "codemodel", "version": 2},
{"kind": "cache", "version": 2},
{"kind": "cmakeFiles", "version": 1}
]
}''')
}"""
)
proc = subprocess.run([self._cmake, str(self._build)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf-8',
universal_newlines=True)
proc = subprocess.run(
[self._cmake, str(self._build)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
universal_newlines=True,
)
self.query_json.unlink()
self.query_json.parent.rmdir()
if proc.returncode != 0:
logging.error(
f'cmake exited with {proc.returncode}: {proc.stderr}')
logging.error(f"cmake exited with {proc.returncode}: {proc.stderr}")
return False
return True
def read_reply(self) -> bool:
reply = self._build / '.cmake' / 'api' / 'v1' / 'reply'
indices = sorted(reply.glob('index-*.json'))
"""Reads the CMake file API reply file and updates internal state
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:
logger.error('no reply')
logger.error("no reply")
return False
with indices[-1].open() as fp:
index = json.load(fp)
try:
responses = index['reply'][f'client-{self._uuid}']['query.json'][
'responses']
responses = index["reply"][f"client-{self._uuid}"]["query.json"][
"responses"
]
except KeyError:
logger.error('no rensponse')
logger.error("no rensponse")
return False
for response in responses:
if response['kind'] == 'codemodel':
self._read_codemodel(reply / response['jsonFile'])
elif response['kind'] == 'cache':
self._read_cache(reply / response['jsonFile'])
elif response['kind'] == 'cmakeFiles':
self._read_cmake_files(reply / response['jsonFile'])
if response["kind"] == "codemodel":
self._read_codemodel(reply / response["jsonFile"])
elif response["kind"] == "cache":
self._read_cache(reply / response["jsonFile"])
elif response["kind"] == "cmakeFiles":
self._read_cmake_files(reply / response["jsonFile"])
return True
def _read_codemodel(self, codemodelpath: Path):
def _read_codemodel(self, codemodelpath: Path) -> None:
with (codemodelpath).open() as fp:
codemodel = json.load(fp)
config = codemodel['configurations'][0]
self._targets[:] = [x['name'] for x in config['targets']]
config = codemodel["configurations"][0]
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:
cache = json.load(fp)
self._cached_variables.clear()
for entry in cache['entries']:
name = entry['name']
value = self._truncate_variable(entry['value'])
properties = {x['name']: x['value'] for x in entry['properties']}
helpstring = properties.get('HELPSTRING', '')
for entry in cache["entries"]:
name = entry["name"]
value = self._truncate_variable(entry["value"])
properties = {x["name"]: x["value"] for x in entry["properties"]}
helpstring = properties.get("HELPSTRING", "")
doc = []
if helpstring:
doc.append(helpstring)
if value:
doc.append(f'`{value}`')
self._cached_variables[name] = '\n\n'.join(doc)
doc.append(f"`{value}`")
self._cached_variables[name] = "\n\n".join(doc)
def _read_cmake_files(self, jsonpath: Path):
'''inspect generated list files'''
def _read_cmake_files(self, jsonpath: Path) -> None:
"""inspect CMake list files that are used during build generation"""
if not self._builtin_variables or self._generated_list_parsed:
return
@@ -128,46 +143,48 @@ class API(object):
with jsonpath.open() as 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:
tmplist = Path(tmpdirname) / 'dump.cmake'
with tmplist.open('w') as fp:
for listfile in cmake_files['inputs']:
if not listfile.get('isGenerated', False):
tmplist = Path(tmpdirname) / "dump.cmake"
with tmplist.open("w") as fp:
for listfile in cmake_files["inputs"]:
if not listfile.get("isGenerated", False):
continue
path = listfile['path']
fp.write(f'include({path})\n')
fp.write('''
path = listfile["path"]
fp.write(f"include({path})\n")
fp.write(
"""
get_cmake_property(variables VARIABLES)
foreach (variable ${variables})
message("${variable}=${${variable}}")
endforeach()
''')
"""
)
p = subprocess.run(
[self._cmake, '-P', str(tmplist)],
[self._cmake, "-P", str(tmplist)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cmake_files['paths']['source'],
encoding='utf-8',
universal_newlines=True)
cwd=cmake_files["paths"]["source"],
encoding="utf-8",
universal_newlines=True,
)
if p.returncode != 0:
return
for line in p.stderr.split('\n'):
for line in p.stderr.split("\n"):
line = line.strip()
if not line:
continue
k, v = line.split('=', 1)
if k.startswith('CMAKE_ARG'):
k, v = line.split("=", 1)
if k.startswith("CMAKE_ARG"):
continue
v = self._truncate_variable(v)
if k in self._builtin_variables:
self._builtin_variables[k] += f'\n\n`{v}`'
self._builtin_variables[k] += f"\n\n`{v}`"
else:
for pattern, doc in self._builtin_variable_template.items(
):
for pattern, doc in self._builtin_variable_template.items():
if pattern.fullmatch(k):
self._builtin_variables[k] = f'{doc}\n\n`{v}`'
self._builtin_variables[k] = f"{doc}\n\n`{v}`"
break
else:
# ignore variable with no document
@@ -177,12 +194,19 @@ endforeach()
@property
def query_json(self) -> Path:
return (self._build / '.cmake' / 'api' / 'v1' / 'query' /
f'client-{self._uuid}' / 'query.json')
return (
self._build
/ ".cmake"
/ "api"
/ "v1"
/ "query"
/ f"client-{self._uuid}"
/ "query.json"
)
@property
def cmake_cache(self) -> Path:
return self._build / 'CMakeCache.txt'
return self._build / "CMakeCache.txt"
def parse_doc(self) -> None:
self._parse_commands()
@@ -190,81 +214,110 @@ endforeach()
self._parse_modules()
def _parse_commands(self) -> None:
p = subprocess.run([self._cmake, '--help-commands'],
stdout=subprocess.PIPE,
encoding='utf-8',
universal_newlines=True)
"""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,
encoding="utf-8",
universal_newlines=True,
)
if p.returncode != 0:
return
matches = re.finditer(
r'''
r"""
(?P<command>.+)\n
-+\n+?
[\s\S]*?
(?P<signature>(?P=command)\s*\([^)]*\))
''', p.stdout, re.VERBOSE)
""",
p.stdout,
re.VERBOSE,
)
self._builtin_commands.clear()
for match in matches:
command = match.group('command')
signature = match.group('signature')
signature = re.sub(r'^ ', r'', signature, flags=re.MULTILINE)
self._builtin_commands[
command] = '```cmake\n' + signature + '\n```'
command = match.group("command")
signature = match.group("signature")
signature = re.sub(r"^ ", r"", signature, flags=re.MULTILINE)
self._builtin_commands[command] = "```cmake\n" + signature + "\n```"
def _parse_variables(self) -> None:
p = subprocess.run([self._cmake, '--help-variables'],
stdout=subprocess.PIPE,
encoding='utf-8',
universal_newlines=True)
"""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,
encoding="utf-8",
universal_newlines=True,
)
if p.returncode != 0:
return
matches = re.finditer(
r'''
r"""
(?P<variable>.+)\n
-+\n\n
(?P<doc>[\s\S]+?)(?:\n\n|$)
''', p.stdout, re.VERBOSE)
""",
p.stdout,
re.VERBOSE,
)
self._builtin_variables.clear()
for match in matches:
variable = match.group('variable')
doc = _tidy_doc(match.group('doc'))
if variable == 'CMAKE_MATCH_<n>':
variable = match.group("variable")
doc = _tidy_doc(match.group("doc"))
if variable == "CMAKE_MATCH_<n>":
for i in range(10):
self._builtin_variables[f'CMAKE_MATCH_{i}'] = doc
elif '<' in variable:
variable = re.sub(r'<[^>]+>', r'[^_]+', variable)
self._builtin_variables[f"CMAKE_MATCH_{i}"] = doc
elif "<" in variable:
variable = re.sub(r"<[^>]+>", r"[^_]+", variable)
pattern = re.compile(variable)
self._builtin_variable_template[pattern] = doc
else:
self._builtin_variables[variable] = doc
def _parse_modules(self) -> None:
p = subprocess.run([self._cmake, '--help-modules'],
stdout=subprocess.PIPE,
encoding='utf-8',
universal_newlines=True)
"""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'''
r"""
(?P<module>.+)\n
-+\n+?
(?:(?P<header>\w[\w\s]+)\n\^+\n+?)?
(?P<doc>.(?:.|\n)+?\n\n)
''', p.stdout + '\n\n', re.VERBOSE)
(?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 is not None and header != 'Overview':
doc = ''
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]:
@@ -281,28 +334,27 @@ endforeach()
return self._builtin_variables.get(variable)
def search_variable(self, variable: str) -> List[str]:
cached = frozenset(x for x in self._cached_variables
if x.startswith(variable))
builtin = frozenset(x for x in self._builtin_variables
if x.startswith(variable))
cached = frozenset(x for x in self._cached_variables if x.startswith(variable))
builtin = frozenset(
x for x in self._builtin_variables if x.startswith(variable)
)
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("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)
]
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')
x
for x in self._builtin_modules
if x.startswith(module) and not x.startswith("Find")
]
def search_target(self, target: str) -> List[str]:
@@ -310,4 +362,4 @@ endforeach()
def _truncate_variable(self, v: str) -> str:
width = 70
return v[:width] + (v[width:] and '...')
return v[:width] + (v[width:] and "...")

View File

@@ -1,117 +1,126 @@
from typing import List
from typing import List, Optional
from .parser import TokenList
class Formatter(object):
indnt: str
indent: str
lower_identifier: bool
def __init__(self, indent=' ', lower_identifier=True):
def __init__(self, indent: str = " ", lower_identifier: bool = True):
self.indent = indent
self.lower_identifier = lower_identifier
def format(self, tokens: TokenList) -> str:
cmds: List[str] = ['']
indnet_level = 0
cmds: List[str] = [""]
indent_level = 0
for token in tokens:
if isinstance(token, tuple):
raw_identifier = token[0]
identifier = raw_identifier.lower()
if identifier in ('elseif', 'else', 'endif', 'endforeach',
'endwhile', 'endmacro', 'endfunction'):
if indnet_level > 0:
indnet_level -= 1
cmds[-1] = self.indent * indnet_level
cmds[-1] += (identifier
if self.lower_identifier else raw_identifier)
if identifier in (
"elseif",
"else",
"endif",
"endforeach",
"endwhile",
"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])
if len(args) < 2:
cmds[-1] += '(' + ''.join(args) + ')'
cmds[-1] += "(" + "".join(args) + ")"
else:
cmds[-1] += '(\n'
cmds[-1] += "(\n"
for arg in args:
cmds[-1] += self.indent * (indnet_level +
1) + arg + '\n'
cmds[-1] += self.indent * indnet_level + ')'
if identifier in ('if', 'elseif', 'else', 'foreach', 'while',
'macro', 'function'):
indnet_level += 1
elif token == '\n':
cmds.append('')
elif token[0] == '#':
cmds[-1] += self.indent * (indent_level + 1) + arg + "\n"
cmds[-1] += self.indent * indent_level + ")"
if identifier in (
"if",
"elseif",
"else",
"foreach",
"while",
"macro",
"function",
):
indent_level += 1
elif token == "\n":
cmds.append("")
elif token[0] == "#":
if cmds[-1]:
cmds[-1] += token
else:
cmds[-1] = self.indent * indnet_level + token
cmds[-1] = self.indent * indent_level + token
elif cmds[-1]:
cmds[-1] += token
cmds = self._strip_line(cmds)
return '\n'.join(cmds) + '\n'
return "\n".join(cmds) + "\n"
def _format_args(self, args: List[str]) -> List[str]:
lines = ['']
lines = [""]
for i in range(len(args)):
arg = args[i]
if arg[0] == '#':
if arg[0] == "#":
lines[-1] += arg
elif arg[0] == '\n':
lines.append('')
elif arg[0] == "\n":
lines.append("")
elif arg.isspace():
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
else:
lines[-1] += ' '
lines[-1] += " "
else:
lines[-1] += arg
return self._strip_line(lines)
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] = []
for line in lines:
line = line.rstrip()
if line != '' or len(ret) > 0:
if line != "" or len(ret) > 0:
ret.append(line)
while ret and ret[-1] == '':
while ret and ret[-1] == "":
del ret[-1]
return ret
def main(args: List[str] = None):
def main(argss: Optional[List[str]] = None) -> None:
import sys
from argparse import ArgumentParser
from difflib import unified_diff
from pathlib import Path
import sys
from . import __version__
from .parser import ListParser
parser = ArgumentParser(
description='Format CMake list files.',
epilog='''
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.''',
standard input and writes the result to the standard output.""",
)
parser.add_argument('lists', type=Path, nargs='*', help='CMake list files')
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__}')
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(args)
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)
print("error: cannot use -i when no arguments are specified.", file=sys.stderr)
return
if not args.lists:
args.lists.append(None)
@@ -120,7 +129,7 @@ def main(args: List[str] = None):
formatter = Formatter()
for listpath in args.lists:
if listpath is None:
listpath = '(stdin)'
listpath = "(stdin)"
content = sys.stdin.read()
else:
with listpath.open() as fp:
@@ -130,14 +139,18 @@ def main(args: List[str] = None):
if args.inplace:
if not remain:
with listpath.open('w') as fp:
with listpath.open("w") as fp:
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='')
diff = unified_diff(
content.splitlines(True),
formatted.splitlines(True),
str(listpath),
str(listpath),
"(before formatting)",
"(after formatting)",
)
diffstr = "".join(diff)
print(diffstr, end="")
else:
print(formatted, end='')
print(formatted, end="")

View File

@@ -10,9 +10,9 @@ TokenList = List[TokenType]
class ListParser(object):
_parser: pp.ParserElement
def __init__(self):
newline = '\n'
space_plus = pp.Regex('[ \t]+')
def __init__(self) -> None:
newline = "\n"
space_plus = pp.Regex("[ \t]+")
space_star = pp.Optional(space_plus)
quoted_element = pp.Regex(r'[^\\"]|\\[^A-Za-z0-9]|\\[trn]')
@@ -20,12 +20,12 @@ class ListParser(object):
bracket_content = pp.Forward()
def action_bracket_open(tokens: pp.ParseResults):
def action_bracket_open(tokens: pp.ParseResults) -> None:
nonlocal bracket_content
marker = ']' + '=' * (len(tokens[0]) - 2) + ']'
marker = "]" + "=" * (len(tokens[0]) - 2) + "]"
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)
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
line_comment = pp.Combine('#' + ~bracket_open +
pp.SkipTo(pp.LineEnd()))
bracket_comment = pp.Combine('#' + bracket_argument)
line_ending = (space_star +
pp.ZeroOrMore(bracket_comment + space_star) +
pp.Optional(line_comment) + (newline | pp.lineEnd))
line_comment = pp.Combine("#" + ~bracket_open + pp.SkipTo(pp.LineEnd()))
bracket_comment = pp.Combine("#" + bracket_argument)
line_ending = (
space_star
+ pp.ZeroOrMore(bracket_comment + space_star)
+ 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.ZeroOrMore(argument | line_ending | space_plus
| '(' + arguments + ')').leaveWhitespace()
arguments << pp.ZeroOrMore(
argument | line_ending | space_plus | "(" + arguments + ")"
).leaveWhitespace()
arguments = pp.Group(arguments)
PAREN_L, PAREN_R = map(pp.Suppress, '()')
PAREN_L, PAREN_R = map(pp.Suppress, "()")
command_invocation = (
identifier + space_star.suppress() + PAREN_L + arguments +
PAREN_R).setParseAction(lambda t: (t[0], t[1].asList()))
identifier + space_star.suppress() + PAREN_L + arguments + PAREN_R
).setParseAction(lambda t: (t[0], t[1].asList()))
file_element = (space_star + command_invocation + line_ending
| line_ending).leaveWhitespace()
file_element = (
space_star + command_invocation + line_ending | line_ending
).leaveWhitespace()
file = pp.ZeroOrMore(file_element)
self._parser = file

View File

@@ -1,16 +1,35 @@
import logging
import re
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,
INITIALIZED, TEXT_DOCUMENT_DID_SAVE)
from pygls.lsp.methods import (
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.types import (CompletionItem, CompletionItemKind, CompletionList,
CompletionParams, CompletionTriggerKind,
DocumentFormattingParams, Hover, InitializeParams,
MarkupContent, MarkupKind, Position, Range,
TextDocumentPositionParams, TextEdit)
from .api import API
from .formatter import Formatter
@@ -21,44 +40,51 @@ logger = logging.getLogger(__name__)
class CMakeLanguageServer(LanguageServer):
_parser: ListParser
_api: API
_api: Optional[API]
def __init__(self, *args):
def __init__(self, *args: Any) -> None:
super().__init__(*args)
self._parser = ListParser()
self._api = None
@self.feature(INITIALIZE)
def initialize(params: InitializeParams):
opts = params.initializationOptions
def initialize(params: InitializeParams) -> None:
opts = params.initialization_options
cmake = getattr(opts, 'cmakeExecutable', 'cmake')
builddir = getattr(opts, 'buildDirectory', '')
logging.info(f'cmakeExecutable={cmake}, buildDirectory={builddir}')
cmake = getattr(opts, "cmakeExecutable", "cmake")
builddir = getattr(opts, "buildDirectory", "")
logging.info(f"cmakeExecutable={cmake}, buildDirectory={builddir}")
self._api = API(cmake, Path(builddir))
self._api.parse_doc()
trigger_characters = ['{', '(']
trigger_characters = ["{", "("]
@self.feature(COMPLETION, trigger_characters=trigger_characters)
def completions(params: CompletionParams):
if (hasattr(params, 'context') and params.context.triggerKind ==
CompletionTriggerKind.TriggerCharacter):
token = ''
trigger = params.context.triggerCharacter
@self.feature(
COMPLETION, CompletionOptions(trigger_characters=trigger_characters)
)
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:
line = self._cursor_line(params.textDocument.uri,
params.position)
line = self._cursor_line(params.text_document.uri, params.position)
idx = params.position.character - 1
if 0 <= idx < len(line) and line[idx] in trigger_characters:
token = ''
token = ""
trigger = line[idx]
else:
word = self._cursor_word(params.textDocument.uri,
params.position, False)
token = '' if word is None else word[0]
word = self._cursor_word(
params.text_document.uri, params.position, False
)
token = "" if word is None else word[0]
trigger = None
items: List[CompletionItem] = []
@@ -66,131 +92,166 @@ class CMakeLanguageServer(LanguageServer):
if trigger is None:
commands = self._api.search_command(token)
items.extend(
CompletionItem(x,
CompletionItemKind.Function,
documentation=self._api.get_command_doc(x),
insert_text=x) for x in commands)
CompletionItem(
label=x,
kind=CompletionItemKind.Function,
documentation=self._api.get_command_doc(x),
insert_text=x,
)
for x in commands
)
if trigger is None or trigger == '{':
if trigger is None or trigger == "{":
variables = self._api.search_variable(token)
items.extend(
CompletionItem(x,
CompletionItemKind.Variable,
documentation=self._api.get_variable_doc(x),
insert_text=x) for x in variables)
CompletionItem(
label=x,
kind=CompletionItemKind.Variable,
documentation=self._api.get_variable_doc(x),
insert_text=x,
)
for x in variables
)
if trigger is None:
targets = self._api.search_target(token)
items.extend(
CompletionItem(x, CompletionItemKind.Class, insert_text=x)
for x in targets)
CompletionItem(
lable=x, kind=CompletionItemKind.Class, insert_text=x
)
for x in targets
)
if trigger == '(':
func = self._cursor_function(params.textDocument.uri,
params.position)
if trigger == "(":
func = self._cursor_function(params.text_document.uri, params.position)
if func is not None:
func = func.lower()
if func == 'include':
if func == "include":
modules = self._api.search_module(token, False)
items.extend(
CompletionItem(x,
CompletionItemKind.Module,
documentation=self._api.
get_module_doc(x, False),
insert_text=x) for x in modules)
elif func == 'find_package':
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(x,
CompletionItemKind.Module,
documentation=self._api.
get_module_doc(x, True),
insert_text=x) for x in modules)
CompletionItem(
label=x,
kind=CompletionItemKind.Module,
documentation=self._api.get_module_doc(x, True),
insert_text=x,
)
for x in modules
)
return CompletionList(False, items)
return CompletionList(is_incomplete=False, items=items)
@self.feature(FORMATTING)
def formatting(params: DocumentFormattingParams):
doc = self.workspace.get_document(params.textDocument.uri)
def formatting(params: DocumentFormattingParams) -> Optional[List[TextEdit]]:
doc = self.workspace.get_document(params.text_document.uri)
content = doc.source
tokens, remain = self._parser.parse(content)
if remain:
self.show_message('CMake parser failed')
self.show_message("CMake parser failed")
return None
formatted = Formatter().format(tokens)
lines = content.count('\n')
lines = content.count("\n")
return [
TextEdit(Range(Position(0, 0), Position(lines + 1, 0)),
formatted)
TextEdit(
range=Range(
start=Position(line=0, character=0),
end=Position(line=lines + 1, character=0),
),
new_text=formatted,
)
]
@self.feature(HOVER)
def hover(params: TextDocumentPositionParams):
word = self._cursor_word(params.textDocument.uri, params.position,
True)
def hover(params: TextDocumentPositionParams) -> Optional[Hover]:
assert self._api is not None
api = self._api
word = self._cursor_word(params.text_document.uri, params.position, True)
if not word:
return None
candidates = [
lambda x: self._api.get_command_doc(x.lower()),
lambda x: self._api.get_variable_doc(x),
lambda x: self._api.get_module_doc(x, False),
lambda x: self._api.get_module_doc(x, True),
candidates: List[Callable[[str], Optional[str]]] = [
lambda x: api.get_command_doc(x.lower()),
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(MarkupContent(MarkupKind.Markdown, doc), word[1])
return Hover(
contents=MarkupContent(kind=MarkupKind.Markdown, value=doc),
range=word[1],
)
return None
@self.thread()
@self.feature(TEXT_DOCUMENT_DID_SAVE, includeText=False)
@self.feature(
TEXT_DOCUMENT_DID_SAVE,
TextDocumentSaveRegistrationOptions(include_text=False),
)
@self.feature(INITIALIZED)
def run_cmake(*args):
def run_cmake(*args: Any) -> None:
assert self._api is not None
if self._api.query():
self._api.read_reply()
def _cursor_function(self, uri: str, position: Position) -> Optional[str]:
doc = self.workspace.get_document(uri)
lines = doc.source.split('\n')[:position.line + 1]
lines[-1] = lines[-1][:position.character - 1].strip()
words = re.split(r'[\s\n()]+', '\n'.join(lines))
lines = doc.source.split("\n")[: position.line + 1]
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)
content = doc.source
line = content.split('\n')[position.line]
return line
line = content.split("\n")[position.line]
return str(line)
def _cursor_word(self,
uri: str,
position: Position,
include_all: bool = True) -> Optional[Tuple[str, Range]]:
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):
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(Position(position.line, m.start()),
Position(position.line, 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
def main(args=None):
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__}')
args = parser.parse_args(args)
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.getLogger('pygls').setLevel(logging.WARNING)
CMakeLanguageServer().start_io()
logging.getLogger("pygls").setLevel(logging.WARNING)
CMakeLanguageServer().start_io() # type: ignore

View File

@@ -2,46 +2,49 @@ 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
from pygls import features
from pygls.server import LanguageServer
from cmake_language_server.server import CMakeLanguageServer
from pygls.lsp.methods import EXIT
from pygls.server import LanguageServer
@pytest.fixture()
def cmake_build(shared_datadir):
source = shared_datadir / 'cmake'
build = source / 'build'
def cmake_build(shared_datadir: Path) -> Iterable[Path]:
source = shared_datadir / "cmake"
build = source / "build"
build.mkdir()
p = run(['cmake', str(source)],
cwd=build,
stdout=PIPE,
stderr=PIPE,
universal_newlines=True)
p = run(
["cmake", str(source)],
cwd=build,
stdout=PIPE,
stderr=PIPE,
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)
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
@pytest.fixture()
def client_server():
def client_server() -> Iterable[Tuple[LanguageServer, CMakeLanguageServer]]:
c2s_r, c2s_w = os.pipe()
s2c_r, s2c_w = os.pipe()
def start(ls: LanguageServer, fdr, fdw):
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
ls.start_io(os.fdopen(fdr, 'rb'), os.fdopen(fdw, 'wb'))
ls.loop.close = 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))
@@ -53,7 +56,7 @@ def client_server():
yield client, server
client.send_notification(features.EXIT)
server.send_notification(features.EXIT)
client.send_notification(EXIT)
server.send_notification(EXIT)
server_thread.join()
client_thread.join()

View File

@@ -1,110 +1,119 @@
import subprocess
from pathlib import Path
from cmake_language_server.api import API
def test_query_with_cache(cmake_build):
api = API('cmake', cmake_build)
def test_query_with_cache(cmake_build: Path) -> None:
api = API("cmake", cmake_build)
assert api.query()
query = cmake_build / '.cmake' / 'api' / 'v1' / 'query'
query = cmake_build / ".cmake" / "api" / "v1" / "query"
assert query.exists()
reply = cmake_build / '.cmake' / 'api' / 'v1' / 'reply'
reply = cmake_build / ".cmake" / "api" / "v1" / "reply"
assert reply.exists()
def test_query_without_cache(cmake_build):
api = API('cmake', cmake_build)
(cmake_build / 'CMakeCache.txt').unlink()
def test_query_without_cache(cmake_build: Path) -> None:
api = API("cmake", cmake_build)
(cmake_build / "CMakeCache.txt").unlink()
assert not api.query()
def test_read_variable(cmake_build):
api = API('cmake', cmake_build)
def test_read_variable(cmake_build: Path) -> None:
api = API("cmake", cmake_build)
assert api.query()
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):
api = API('cmake', cmake_build)
def test_read_cmake_files(cmake_build: Path) -> None:
api = API("cmake", cmake_build)
api.parse_doc()
assert api.query()
api.read_reply()
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')
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')
raise RuntimeError("Unexpected system")
def test_parse_commands(cmake_build):
api = API('cmake', cmake_build)
def test_parse_commands(cmake_build: Path) -> None:
api = API("cmake", cmake_build)
api.parse_doc()
p = subprocess.run(['cmake', '--help-command-list'],
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
commands = p.stdout.strip().split('\n')
p = subprocess.run(
["cmake", "--help-command-list"],
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
commands = p.stdout.strip().split("\n")
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 api.get_command_doc('not_existing_command') is None
assert "break()" in api.get_command_doc("break")
assert api.get_command_doc("not_existing_command") is None
def test_parse_variables(cmake_build):
api = API('cmake', cmake_build)
def test_parse_variables(cmake_build: Path) -> None:
api = API("cmake", cmake_build)
api.parse_doc()
p = subprocess.run(['cmake', '--help-variable-list'],
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
variables = p.stdout.strip().split('\n')
p = subprocess.run(
["cmake", "--help-variable-list"],
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
variables = p.stdout.strip().split("\n")
for variable in variables:
if '<' in variable:
if "<" in variable:
continue
assert api.get_variable_doc(
variable) is not None, f'{variable} not found'
assert api.get_variable_doc(variable) is not None, f"{variable} not found"
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("BUILD_SHARED_LIBS") is not None
assert api.get_variable_doc("not_existing_variable") is None
def test_parse_modules(cmake_build):
api = API('cmake', cmake_build)
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')
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'
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(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']
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"]

View File

@@ -1,13 +1,16 @@
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 pytest import CaptureFixture
def make_formatter_test(liststr: str, expect: str):
def test():
def make_formatter_test(liststr: str, expect: str) -> Callable[[], None]:
def test() -> None:
tokens, remain = ListParser().parse(liststr)
actual = Formatter().format(tokens)
assert actual == expect
@@ -15,50 +18,57 @@ def make_formatter_test(liststr: str, expect: str):
return test
test_command = make_formatter_test('a()', 'a()\n')
test_command_tolower = make_formatter_test('A()', 'a()\n')
test_remove_space = make_formatter_test('''
test_command = make_formatter_test("a()", "a()\n")
test_command_tolower = make_formatter_test("A()", "a()\n")
test_remove_space = make_formatter_test(
"""
#a
b ( c ) # d
''', '''\
""",
"""\
#a
b(c) # d
''')
""",
)
test_indent_if = make_formatter_test(
'''
"""
if()
a() # a
else()
# b
b()
endif()
''', '''\
""",
"""\
if()
a() # a
else()
# b
b()
endif()
''')
""",
)
test_indent_if_nested = make_formatter_test(
'''
"""
if()
if()
a()
b()
endif()
endif()
''', '''\
""",
"""\
if()
if()
a()
b()
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(
'''
"""
if()
a(b c
d # e
@@ -66,7 +76,8 @@ f
# g
) # h
endif()
''', '''\
""",
"""\
if()
a(
b c
@@ -75,82 +86,83 @@ if()
# g
) # h
endif()
''')
""",
)
@contextmanager
def mock_stdin(buf: str):
def mock_stdin(buf: str) -> Iterator[None]:
stdin = sys.stdin
sys.stdin = StringIO(buf)
yield
sys.stdin = stdin
def test_main_stdin(capsys):
with mock_stdin(' a()'):
def test_main_stdin(capsys: CaptureFixture[str]) -> None:
with mock_stdin(" a()"):
main([])
captured = capsys.readouterr()
assert captured.out == 'a()\n'
assert captured.err == ''
assert captured.out == "a()\n"
assert captured.err == ""
def test_main_stdin_diff(capsys):
with mock_stdin(' a()'):
main(['-d'])
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 == ''
assert "- a()" in captured.out
assert "+a()" in captured.out
assert captured.err == ""
def test_main_file_1(capsys, tmp_path):
testfile1 = tmp_path / 'list1.cmake'
with testfile1.open('w') as fp:
fp.write(' a()')
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 == ''
assert captured.out == "a()\n"
assert captured.err == ""
def test_main_file_2(capsys, tmp_path):
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()')
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 == ''
assert captured.out == "a()\nb()\n"
assert captured.err == ""
def test_main_inplace(capsys, tmp_path):
testfile1 = tmp_path / 'list1.cmake'
with testfile1.open('w') as fp:
fp.write(' a()')
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)])
main(["-i", str(testfile1)])
captured = capsys.readouterr()
assert captured.out == ''
assert captured.err == ''
assert captured.out == ""
assert captured.err == ""
with testfile1.open() as fp:
content = fp.read()
assert content == 'a()\n'
assert content == "a()\n"
def test_main_diff(capsys, tmp_path):
testfile1 = tmp_path / 'list1.cmake'
with testfile1.open('w') as fp:
fp.write(' a()')
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)])
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 == ''
assert "- a()" in captured.out
assert "+a()" in captured.out
assert captured.err == ""

View File

@@ -1,12 +1,12 @@
from typing import List
from typing import Callable, List
from cmake_language_server.parser import ListParser, TokenType
def make_parser_test(liststr: str,
expect_token: List[TokenType],
expect_remain: str = ''):
def test():
def make_parser_test(
liststr: str, expect_token: List[TokenType], expect_remain: str = ""
) -> Callable[[], None]:
def test() -> None:
actual_token, actual_remain = ListParser().parse(liststr)
assert actual_token == expect_token
assert actual_remain == expect_remain
@@ -14,51 +14,70 @@ def make_parser_test(liststr: str,
return test
test_command_no_args = 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_space = make_parser_test('a ( b )', [('a', ['b'])])
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_no_args = 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_space = make_parser_test("a ( b )", [("a", ["b"])])
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_paren = make_parser_test(
'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")',
[('a', ['"\\\n"'])])
test_command_arg_quo_multiline = make_parser_test('''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")', [("a", ['"\\\n"'])])
test_command_arg_quo_multiline = make_parser_test(
"""a("b
c
")''', [('a', ['"b\nc\n"'])])
test_command_arg_bracket_0 = make_parser_test('a([[b]])', [('a', ['[[b]]'])])
test_command_arg_bracket_1 = make_parser_test('a([=[b]=])',
[('a', ['[=[b]=]'])])
test_command_arg_space = make_parser_test('a ( b )', [('a', [' ', 'b', ' '])])
test_command_arg_multi = make_parser_test('a(b c)', [('a', ['b', ' ', 'c'])])
test_command_multielement = make_parser_test('''a(
")""",
[("a", ['"b\nc\n"'])],
)
test_command_arg_bracket_0 = make_parser_test("a([[b]])", [("a", ["[[b]]"])])
test_command_arg_bracket_1 = make_parser_test("a([=[b]=])", [("a", ["[=[b]=]"])])
test_command_arg_space = make_parser_test("a ( b )", [("a", [" ", "b", " "])])
test_command_arg_multi = make_parser_test("a(b c)", [("a", ["b", " ", "c"])])
test_command_multielement = make_parser_test(
"""a(
b
c # c
)''', [('a', ['\n', ' ', 'b', '\n', ' ', 'c', ' ', '# c', '\n'])])
test_line_comment = make_parser_test('a() # b # c',
[('a', []), ' ', '# b # c'])
test_bracket_comment = make_parser_test('#[[a]]#[[b]]', ['#[[a]]', '#[[b]]'])
test_bracket_comment_nested = make_parser_test('#[=[[[a]]]=]',
['#[=[[[a]]]=]'])
test_bracket_comment_multiline = make_parser_test('#[[\na\nb\nc\n]]',
['#[[\na\nb\nc\n]]'])
test_if_block = make_parser_test('''if()
)""",
[("a", ["\n", " ", "b", "\n", " ", "c", " ", "# c", "\n"])],
)
test_line_comment = make_parser_test("a() # b # c", [("a", []), " ", "# b # c"])
test_bracket_comment = make_parser_test("#[[a]]#[[b]]", ["#[[a]]", "#[[b]]"])
test_bracket_comment_nested = make_parser_test("#[=[[[a]]]=]", ["#[=[[[a]]]=]"])
test_bracket_comment_multiline = make_parser_test(
"#[[\na\nb\nc\n]]", ["#[[\na\nb\nc\n]]"]
)
test_if_block = make_parser_test(
"""if()
a()
else()
b()
endif()''', [('if', []), '\n', ' ', ('a', []), '\n', ('else', []), '\n', ' ',
('b', []), '\n', ('endif', [])])
endif()""",
[
("if", []),
"\n",
" ",
("a", []),
"\n",
("else", []),
"\n",
" ",
("b", []),
"\n",
("endif", []),
],
)
test_comment_multi_linecomment = make_parser_test(
'''a()# a
"""a()# a
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_command = make_parser_test('a(', [], 'a(')
test_incomplete_id_after_command = make_parser_test('a()\nb',
[('a', []), '\n'], 'b')
test_incomplete_id = make_parser_test("a", [], "a")
test_incomplete_command = make_parser_test("a(", [], "a(")
test_incomplete_id_after_command = make_parser_test("a()\nb", [("a", []), "\n"], "b")
test_incomplete_command_after_command = make_parser_test(
'a()\nb(c', [('a', []), '\n'], 'b(c')
"a()\nb(c", [("a", []), "\n"], "b(c"
)

View File

@@ -1,35 +1,53 @@
from concurrent import futures
from pathlib import Path
from typing import Optional
from typing import Any, Dict, Optional, Tuple
from pygls.features import (COMPLETION, FORMATTING, HOVER, INITIALIZE,
TEXT_DOCUMENT_DID_OPEN)
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
from pygls.types import (CompletionContext, CompletionParams,
CompletionTriggerKind, DidOpenTextDocumentParams,
DocumentFormattingParams, FormattingOptions,
InitializeParams, Position, TextDocumentIdentifier,
TextDocumentItem, TextDocumentPositionParams)
CALL_TIMEOUT = 2
def _init(client: LanguageServer, root: Path):
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=None)).result(timeout=CALL_TIMEOUT)
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):
def _open(client: LanguageServer, path: Path, text: Optional[str] = None) -> None:
if text is None:
with open(path) as fp:
text = fp.read()
@@ -37,25 +55,39 @@ def _open(client: LanguageServer, path: Path, text: Optional[str] = None):
client.lsp.notify(
TEXT_DOCUMENT_DID_OPEN,
DidOpenTextDocumentParams(
TextDocumentItem(path.as_uri(), 'cmake', 1, text)))
text_document=TextDocumentItem(
uri=path.as_uri(), language_id="cmake", version=1, text=text
)
),
)
def _test_completion(client_server, datadir, content: str,
context: Optional[CompletionContext]):
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'
path = datadir / "CMakeLists.txt"
_open(client, path, content)
params = CompletionParams(TextDocumentIdentifier(path.as_uri()),
Position(0, len(content)), context)
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
return client.lsp.send_request(COMPLETION,
params).result(timeout=CALL_TIMEOUT)
ret = client.lsp.send_request(COMPLETION, params).result(timeout=CALL_TIMEOUT)
assert isinstance(ret, dict)
return ret
def test_initialize(client_server, datadir):
def test_initialize(
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
) -> None:
client, server = client_server
assert server._api is None
@@ -63,74 +95,111 @@ def test_initialize(client_server, datadir):
assert server._api is not None
def test_completions_invoked(client_server, datadir):
def test_completions_invoked(
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
) -> None:
response = _test_completion(
client_server, datadir, 'projec',
CompletionContext(CompletionTriggerKind.Invoked))
item = next(filter(lambda x: x.label == 'project', response.items), None)
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 '<PROJECT-NAME>' in item.documentation
assert isinstance(item["documentation"], str)
assert "<PROJECT-NAME>" in item["documentation"]
def test_completions_nocontext(client_server, datadir):
response = _test_completion(client_server, datadir, 'projec', None)
item = next(filter(lambda x: x.label == 'project', response.items), None)
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 '<PROJECT-NAME>' in item.documentation
assert isinstance(item["documentation"], str)
assert "<PROJECT-NAME>" in item["documentation"]
def test_completions_triggercharacter_variable(client_server, datadir):
def test_completions_triggercharacter_variable(
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
) -> None:
response = _test_completion(
client_server, datadir, '${',
CompletionContext(CompletionTriggerKind.TriggerCharacter, '{'))
assert 'PROJECT_VERSION' in [x.label for x in response.items]
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)
response_nocontext = _test_completion(client_server, datadir, "${", None)
assert response == response_nocontext
def test_completions_triggercharacter_module(client_server, datadir):
def test_completions_triggercharacter_module(
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
) -> None:
response = _test_completion(
client_server, datadir, 'include(',
CompletionContext(CompletionTriggerKind.TriggerCharacter, '('))
assert 'GoogleTest' in [x.label for x in response.items]
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)
response_nocontext = _test_completion(client_server, datadir, "include(", None)
assert response == response_nocontext
def test_completions_triggercharacter_package(client_server, datadir):
def test_completions_triggercharacter_package(
client_server: Tuple[LanguageServer, CMakeLanguageServer], datadir: Path
) -> None:
response = _test_completion(
client_server, datadir, 'find_package(',
CompletionContext(CompletionTriggerKind.TriggerCharacter, '('))
assert 'Boost' in [x.label for x in response.items]
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)
response_nocontext = _test_completion(client_server, datadir, "find_package(", None)
assert response == response_nocontext
def test_formatting(client_server, datadir):
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 ) ')
path = datadir / "CMakeLists.txt"
_open(client, path, "a ( b c ) ")
response = client.lsp.send_request(
FORMATTING,
DocumentFormattingParams(TextDocumentIdentifier(path.as_uri()),
FormattingOptions(
2, True))).result(timeout=CALL_TIMEOUT)
assert response[0].newText == 'a(b c)\n'
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, datadir):
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()')
path = datadir / "CMakeLists.txt"
_open(client, path, "project()")
response = client.lsp.send_request(
HOVER,
TextDocumentPositionParams(TextDocumentIdentifier(path.as_uri()),
Position())).result(timeout=CALL_TIMEOUT)
assert '<PROJECT-NAME>' in response.contents.value
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"]

18
tox.ini
View File

@@ -1,16 +1,19 @@
[tox]
isolated_build = True
skipsdist = True
envlist = py36, py37, py38, lint
envlist = py36, py37, py38, py39, lint
[gh-actions]
python =
3.6: py36
3.7: py37, lint
3.8: py38
3.7: py37
3.8: py38, lint
3.9: py39
[testenv]
whitelist_externals = poetry
allowlist_externals =
poetry
git
skip_install = true
passenv = INCLUDE LIB LIBPATH Platform VCTools* VSCMD_* WindowsSDK*
commands_pre =
@@ -20,7 +23,6 @@ commands =
[testenv:lint]
commands =
poetry run isort -c -rc src tests
poetry run yapf -d -r src tests
poetry run flake8 src tests
poetry run mypy src tests
poetry run pysen run format
git diff --exit-code --ignore-submodules
poetry run pysen run lint