Skip to content

Commit

Permalink
🚚 Move api to documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Oct 17, 2023
1 parent 2990ae3 commit c522496
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 98 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exclude: (^templates/.*|.*\.json$)

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: fix-byte-order-marker
Expand All @@ -27,7 +27,7 @@ repos:
hooks:
- id: remove-crlf
- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
rev: v2.2.6
hooks:
- id: codespell
additional_dependencies:
Expand All @@ -47,7 +47,7 @@ repos:
hooks:
- id: check-mailmap
- repo: https://github.com/rhysd/actionlint
rev: v1.6.25
rev: v1.6.26
hooks:
- id: actionlint
- repo: https://github.com/adrienverge/yamllint
Expand All @@ -68,13 +68,13 @@ repos:
- mdformat-black
- mdformat-config
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.9.2
rev: v0.10.0
hooks:
- id: markdownlint-cli2
additional_dependencies:
- markdown-it-texmath@0.9.1
- markdown-it-texmath
- repo: https://github.com/psf/black
rev: 23.7.0
rev: 23.9.1
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
Expand All @@ -88,7 +88,7 @@ repos:
additional_dependencies:
- tomli
- repo: https://github.com/kumaraditya303/mirrors-pyright
rev: v1.1.325
rev: v1.1.329
hooks:
- id: pyright
- repo: https://github.com/PyCQA/bandit
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ exclude_lines = [
]

[tool.bandit.assert_used]
skips = ["*_test.py", "test_*.py"]
skips = ["*_test.py", "*/test_*.py"]

[tool.cibuildwheel]
archs = ["all"]
Expand Down
18 changes: 0 additions & 18 deletions src/autotools_language_server/api/__init__.py

This file was deleted.

85 changes: 85 additions & 0 deletions src/autotools_language_server/documents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
r"""Documents
=============
"""
import json
import os
from typing import Literal

from platformdirs import user_cache_dir

from .autoconf import init_autoconf_document
from .make import init_make_document


def init_document() -> dict[str, tuple[str, str]]:
r"""Init document.
:rtype: dict[str, tuple[str, str]]
"""
result = {}
for k, v in init_autoconf_document().items():
result[k] = (v, "config")
for k, v in init_make_document().items():
result[k] = (v, "make")
return result


def get_document(
method: Literal["builtin", "cache", "system"] = "builtin"
) -> dict[str, tuple[str, str]]:
r"""Get document. ``builtin`` will use builtin autotools.json. ``cache``
will generate a cache from
``${XDG_CACHE_DIRS:-/usr/share}/info/autoconf.info.gz``,
``${XDG_CACHE_DIRS:-/usr/share}/info/automake.info-1.gz``,
``${XDG_CACHE_DIRS:-/usr/share}/info/make.info-2.gz``.
``system`` is same as ``cache`` except it doesn't generate cache. Some
distribution's autotools doesn't contain textinfo. So we use ``builtin`` as
default.
:param method:
:type method: Literal["builtin", "cache", "system"]
:rtype: dict[str, tuple[str, str]]
"""
if method == "builtin":
file = os.path.join(
os.path.join(
os.path.join(os.path.dirname(__file__), "assets"), "json"
),
"autotools.json",
)
with open(file, "r") as f:
document = json.load(f)
elif method == "cache":
from . import init_document

if not os.path.exists(user_cache_dir("autotools.json")):
document = init_document()
with open(user_cache_dir("autotools.json"), "w") as f:
json.dump(document, f)
else:
with open(user_cache_dir("autotools.json"), "r") as f:
document = json.load(f)
else:
from . import init_document

document = init_document()
return document


def check_extension(
path: str,
) -> Literal["config", "make", ""]:
r"""Check extension.
:param path:
:type path: str
:rtype: Literal["config", "make", ""]
"""
if (
path.split(os.path.extsep)[-1] in ["mk"]
or os.path.basename(path).split(os.path.extsep)[0] == "Makefile"
):
return "make"
if os.path.basename(path) == "configure.ac":
return "config"
return ""
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
r"""Api Autoconf
================
r"""Autoconf
============
"""
import os
from glob import glob
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
r"""Api Make
============
r"""Make
========
"""
import os
from glob import glob
Expand Down
72 changes: 4 additions & 68 deletions src/autotools_language_server/server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
r"""Server
==========
"""
import json
import os
import re
from typing import Any, Literal, Tuple
from typing import Any, Tuple
from urllib.parse import unquote, urlparse

from lsprotocol.types import (
Expand All @@ -28,72 +27,11 @@
Range,
TextDocumentPositionParams,
)
from platformdirs import user_cache_dir
from pygls.server import LanguageServer

from ._tree_sitter import get_parser
from .diagnostics import diagnostic


def check_extension(
path: str,
) -> Literal["config", "make", ""]:
r"""Check extension.
:param path:
:type path: str
:rtype: Literal["config", "make", ""]
"""
if (
path.split(os.path.extsep)[-1] in ["mk"]
or os.path.basename(path).split(os.path.extsep)[0] == "Makefile"
):
return "make"
if os.path.basename(path) == "configure.ac":
return "config"
return ""


def get_document(
method: Literal["builtin", "cache", "system"] = "builtin"
) -> dict[str, tuple[str, str]]:
r"""Get document. ``builtin`` will use builtin autotools.json. ``cache``
will generate a cache from
``${XDG_CACHE_DIRS:-/usr/share}/info/autoconf.info.gz``,
``${XDG_CACHE_DIRS:-/usr/share}/info/automake.info-1.gz``,
``${XDG_CACHE_DIRS:-/usr/share}/info/make.info-2.gz``.
``system`` is same as ``cache`` except it doesn't generate cache. Some
distribution's autotools doesn't contain textinfo. So we use ``builtin`` as
default.
:param method:
:type method: Literal["builtin", "cache", "system"]
:rtype: dict[str, tuple[str, str]]
"""
if method == "builtin":
file = os.path.join(
os.path.join(
os.path.join(os.path.dirname(__file__), "assets"), "json"
),
"autotools.json",
)
with open(file, "r") as f:
document = json.load(f)
elif method == "cache":
from .api import init_document

if not os.path.exists(user_cache_dir("autotools.json")):
document = init_document()
with open(user_cache_dir("autotools.json"), "w") as f:
json.dump(document, f)
else:
with open(user_cache_dir("autotools.json"), "r") as f:
document = json.load(f)
else:
from .api import init_document

document = init_document()
return document
from .documents import check_extension, get_document


class AutotoolsLanguageServer(LanguageServer):
Expand Down Expand Up @@ -249,10 +187,8 @@ def _cursor_word(
word = (
line[m.start() : end],
Range(
start=Position(
line=position.line, character=m.start()
),
end=Position(line=position.line, character=end),
Position(position.line, m.start()),
Position(position.line, end),
),
)
return word
Expand Down

0 comments on commit c522496

Please sign in to comment.