Skip to content

Commit 6fa838e

Browse files
committed
Minor fixes
1 parent 209518e commit 6fa838e

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- Added `lua_ls_max_version` config option to safeguard against incompatible changes
1414
to documentation export format.
1515

16+
- Fixed a few more minor bugs in highlighting of Lua types.
17+
1618
## [3.4.0]
1719

1820
- **Potential breaking change:** use `confdir` instead of `srcdir` as base path

sphinx_lua_ls/domain.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def run(self) -> list[nodes.Node]:
384384
def parse_signature(self, sig: str) -> tuple[str, T]:
385385
raise NotImplementedError()
386386

387-
def use_semicolon_path(self) -> bool:
387+
def use_colon_path(self) -> bool:
388388
return False
389389

390390
def handle_signature_prefix(
@@ -403,7 +403,7 @@ def handle_signature_prefix(
403403
prefix = "" if classname else ".".join(filter(None, [modname, classname]))
404404

405405
descname = name
406-
if self.use_semicolon_path():
406+
if self.use_colon_path():
407407
if "[" in descname:
408408
descname_components = utils.separate_sig(descname, ".")
409409
else:
@@ -696,7 +696,7 @@ def handle_signature(
696696
def needs_arg_list(self) -> bool:
697697
return True
698698

699-
def use_semicolon_path(self) -> bool:
699+
def use_colon_path(self) -> bool:
700700
return self.objtype in ("method", "classmethod")
701701

702702
def get_signature_prefix(
@@ -724,9 +724,6 @@ class LuaData(LuaObject[str]):
724724
def parse_signature(self, sig):
725725
name, sig = utils.separate_name_prefix(sig)
726726

727-
if sig.startswith("=") or sig.startswith(":"):
728-
sig = sig[1:]
729-
730727
return name, sig.strip()
731728

732729
@utils.handle_signature_errors
@@ -740,7 +737,15 @@ def handle_signature(
740737
sw = _SigWriter(signode, self.maximum_signature_line_length)
741738

742739
if typ:
743-
sw.punctuation(":")
740+
if typ.startswith("="):
741+
sw.space()
742+
sw.punctuation("=")
743+
typ = typ[1:]
744+
elif typ.startswith(":"):
745+
sw.punctuation(":")
746+
typ = typ[1:]
747+
else:
748+
sw.punctuation(":")
744749
sw.space()
745750
sw.typ(typ, self.state.inliner)
746751

sphinx_lua_ls/utils.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
#: Regexp for parsing a single Lua identifier.
14-
_OBJECT_NAME_RE = re.compile(r"^\s*(?P<name>[\w-]+)")
14+
_OBJECT_NAME_RE = re.compile(r"^\s*(?P<name>[\w-]+)\s*")
1515

1616
#: A single function parameter name.
1717
_PARAM_NAME_RE = re.compile(r"^\s*[\w-]+\s*$")
@@ -29,7 +29,7 @@ def fn(self, sig: str, signode: addnodes.desc_signature):
2929
sig,
3030
e,
3131
type="lua-ls",
32-
location=(signode.source, signode.line),
32+
location=signode,
3333
)
3434
raise
3535

@@ -49,7 +49,7 @@ def separate_name_prefix(sig: str) -> tuple[str, str]:
4949
name_components.append(f"[{normalize_type(name)}]")
5050
elif match := _OBJECT_NAME_RE.match(sig):
5151
name_components.append(match.group("name"))
52-
sig = sig[match.span()[1] :]
52+
sig = sig[match.end() :]
5353
else:
5454
if seen_dot_prefix:
5555
raise ValueError("incorrect object name")
@@ -230,32 +230,33 @@ def parse_types(sig: str, parsingFunctionParams: bool = False) -> list[tuple[str
230230
# Ident not followed by an open brace, semicolon, etc.
231231
# Example: `module.Type`.
232232
# Doesn't match: `name?: ...`, `name( ...`, etc.
233-
(?P<ident>\w[\w-]*(?:\.\w[\w-]*)*)
234-
\s*(?P<ident_qm>\??)\s*
235-
(?![:(\w.?-])
233+
(?P<ident>\w[\w-]*\s*(?:\.\s*\w[\w-]*\s*)*)
234+
(?P<ident_qm>(?:\?\s*)*)
235+
(?![:(\w.-])
236236
|
237237
# Built-in type not followed by an open brace, semicolon, etc.
238238
# Example: `string`, `string?`.
239239
# Doesn't match: `string?: ...`, `string( ...`, etc.
240240
(?P<type>nil|any|boolean|string|number|integer|function|table|thread|userdata|lightuserdata)
241-
\b\s*(?P<type_qm>\??)\s*
242-
(?![:(\w.?-])
241+
\b\s*(?P<type_qm>(?:\?\s*)*)
242+
(?![:(\w.-])
243243
|
244244
# Name component, only matches when `ident` and `type` didn't match.
245245
# Example: `string: ...`.
246-
(?P<name>\w[\w.-]*)
246+
(?P<name>\w[\w-]*\s*(?:\.\s*\w[\w-]*\s*)*)
247247
|
248248
# Punctuation that we separate with spaces.
249249
(?P<punct>[=:,|&])
250250
|
251251
# Braces.
252-
(?P<brace>[()[\]{}])
252+
# Match them separately, otherwise greedy `other_punct` might eat functional arrow.
253+
(?P<brace>[()[\]{}<>])
253254
|
254255
# Functional arrow (will be replaced with semicolon).
255256
(?P<arrow>->)
256257
|
257258
# Punctuation that we copy as-is, without adding spaces.
258-
(?P<other_punct>[-!#$%*+/\\;<>?@[\]^_{}~]+)
259+
(?P<other_punct>[-!#$%*+/\\;?@^_~]+)
259260
|
260261
# Anything else is copied as-is.
261262
(?P<other>.)
@@ -285,6 +286,7 @@ def type_to_nodes(typ: str, inliner) -> list[nodes.Node]:
285286
elif text := match.group("type"):
286287
res.append(addnodes.desc_sig_keyword_type(text, text))
287288
if qm := match.group("type_qm"):
289+
qm = re.sub(r"\s", "", qm)
288290
res.append(addnodes.desc_sig_punctuation(qm, qm))
289291
elif text := match.group("string"):
290292
res.append(addnodes.desc_sig_literal_string(text, text))
@@ -293,14 +295,17 @@ def type_to_nodes(typ: str, inliner) -> list[nodes.Node]:
293295
elif text := match.group("ident"):
294296
import sphinx_lua_ls.domain
295297

298+
text = re.sub(r"\s", "", text)
296299
ref_nodes, warn_nodes = sphinx_lua_ls.domain.LuaXRefRole()(
297300
"lua:_auto", text, text, 0, inliner
298301
)
299302
res.extend(ref_nodes)
300303
res.extend(warn_nodes)
301304
if qm := match.group("ident_qm"):
305+
qm = re.sub(r"\s", "", qm)
302306
res.append(addnodes.desc_sig_punctuation(qm, qm))
303307
elif text := match.group("name"):
308+
text = re.sub(r"\s", "", text)
304309
res.append(addnodes.desc_sig_name(text, text))
305310
elif text := match.group("punct"):
306311
if text in "=|&":
@@ -315,7 +320,10 @@ def type_to_nodes(typ: str, inliner) -> list[nodes.Node]:
315320
elif text := match.group("other_punct"):
316321
res.append(addnodes.desc_sig_punctuation(text, text))
317322
elif text := match.group("other"):
318-
res.append(nodes.Text(text))
323+
if res and isinstance(res[-1], nodes.Text):
324+
res[-1] += text
325+
else:
326+
res.append(nodes.Text(text))
319327

320328
return res
321329

@@ -339,17 +347,17 @@ def normalize_type(typ: str) -> str:
339347
elif text := match.group("type"):
340348
res += text
341349
if qm := match.group("type_qm"):
342-
res += qm
350+
res += re.sub(r"\s", "", qm)
343351
elif text := match.group("string"):
344352
res += text
345353
elif text := match.group("number"):
346354
res += text
347355
elif text := match.group("ident"):
348-
res += text
356+
res += re.sub(r"\s", "", text)
349357
if qm := match.group("ident_qm"):
350-
res += qm
358+
res += re.sub(r"\s", "", qm)
351359
elif text := match.group("name"):
352-
res += text
360+
res += re.sub(r"\s", "", text)
353361
elif text := match.group("punct"):
354362
if text in "=|":
355363
res += " "

test/test_regression/doc-directives.html

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,13 +2137,6 @@
21372137
data_type_eq
21382138
</span>
21392139
</span>
2140-
<span class="p">
2141-
<span class="pre">
2142-
:
2143-
</span>
2144-
</span>
2145-
<span class="w">
2146-
</span>
21472140
<span class="w">
21482141
</span>
21492142
<span class="p">

0 commit comments

Comments
 (0)