File: pyls.py

package info (click to toggle)
python-noseofyeti 2.4.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 360 kB
  • sloc: python: 2,581; sh: 31; makefile: 12
file content (45 lines) | stat: -rw-r--r-- 1,429 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import re

from pyls import hookimpl

from noseOfYeti.tokeniser.spec_codec import codec

spec_codec = codec()

regexes = {
    "encoding": re.compile(r"#\s*coding\s*:\s*spec"),
    "first_whitespace": re.compile(r"^(\s*)"),
}


@hookimpl(hookwrapper=True)
def pyls_initialize(config, workspace):
    spec_codec.register(transform=True)
    yield


@hookimpl(hookwrapper=True)
def pyls_document_did_open(config, workspace, document):
    contents = document._source
    lines = contents.split("\n")

    if contents and regexes["encoding"].match(lines[0]):
        translated = spec_codec.translate(contents)
        translated = translated.split("\n")[: len(lines)]

        replacement = []
        for orig, new in zip(lines, translated):
            if new.startswith("class"):
                # We still need the root classes to be at the start of the document
                # So that their parent classes exist at that scope
                replacement.append(new)
            else:
                # Everything else however needs to be at their original indentation
                # So that pyls doesn't get confused by columns
                or_space = regexes["first_whitespace"].search(orig).groups()[0]
                tr_space = regexes["first_whitespace"].search(new).groups()[0]
                replacement.append(f"{or_space}{new[len(tr_space):]}")

        document._source = "\n".join(replacement)

    yield