Skip to content

Commit

Permalink
Complete existing sections within [
Browse files Browse the repository at this point in the history
  • Loading branch information
perrinjerome committed Jun 22, 2024
1 parent 119a41c commit 12b5b81
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
2 changes: 2 additions & 0 deletions profiles/completions/sections.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ option4 = value4
[xsection4]
option5 = value5
option6 = ${s

[]
10 changes: 7 additions & 3 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ and this project adheres to [Semantic Versioning][semver].

## Unreleased

### Added

- completions: complete existing sections within `[` to override existing sections.

## [0.12.0] - 2023-10-08

- support python 3.12
- version up dependencies

## Fixed
### Fixed

- hover: fix crash when opening instance.cfg.in without opening a folder (by updating pygls)


## [0.11.0] - 2023-10-04

## Added
### Added

- code actions: support cancellation of md5sum command

## Fixed
### Fixed

- hover: fix crash when opening instance.cfg.in outside of workspace folder

Expand Down
2 changes: 1 addition & 1 deletion server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The automatic installation does not seem to work with theia and the python egg h

## Completions

- `${` complete sections.
- `${` or `[` complete sections.
- `${section:` complete `section`'s options. If `section` uses a known recipe, dynamic options from the recipe are also completed.
- `${buildout:extends}` completes filenames.
- `${buildout:parts}` and `<=` option completes parts.
Expand Down
15 changes: 15 additions & 0 deletions server/buildoutls/buildout.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
# Matches a comment
comment_re = re.compile(r".*[#;].*")

# Matches a partial section definition like
# [s
# or
# []
partial_section_header_re = re.compile(r"^\[(?P<section>[^\]]*)")

# Filenames of slapos instances, that might be a buildout profile as a buildout template
slapos_instance_profile_filename_re = re.compile(r".*\/instance[^\/]*\.cfg[^\/]*")

Expand Down Expand Up @@ -672,13 +678,22 @@ async def getSymbolAtPosition(self, position: Position) -> Optional[Symbol]:
# have a leading space.
# > value
section_header_match = section_header(line) # reuse buildout's regexp
partial_section_header_match = partial_section_header_re.match(line)
if section_header_match:
return Symbol(
kind=SymbolKind.SectionDefinition,
buildout=self,
value=section_header_match.group("name"),
current_section_name=section_header_match.group("name"),
)
elif partial_section_header_match:
return Symbol(
kind=SymbolKind.SectionDefinition,
buildout=self,
value=partial_section_header_match.group("section"),
current_section_name=partial_section_header_match.group("section"),
)

if option_value_definition_match:
# Single line option and value. The position might be on option
# or value
Expand Down
12 changes: 11 additions & 1 deletion server/buildoutls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def getDefaultTextEdit(
if parsed is None:
return None
symbol = await parsed.getSymbolAtPosition(params.position)
logger.debug("getting completions on %s", symbol)
logger.debug("getting completions at %s => %s", params.position, symbol)
if symbol:
if symbol.kind == buildout.SymbolKind.Comment:
return None
Expand Down Expand Up @@ -594,6 +594,16 @@ def getDefaultTextEdit(
)
)

elif symbol.kind == buildout.SymbolKind.SectionDefinition:
for section_name in symbol._buildout:
items.append(
CompletionItem(
label=section_name,
text_edit=getDefaultTextEdit(doc, params.position, section_name),
kind=CompletionItemKind.Function,
)
)

return items


Expand Down
39 changes: 38 additions & 1 deletion server/buildoutls/tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def test_complete_section_reference(server: LanguageServer):
context = CompletionContext(
trigger_kind=CompletionTriggerKind.Invoked,
)
# complete section names
# complete section names in ${se|}
params = CompletionParams(
text_document=TextDocumentIdentifier(uri="file:///completions/sections.cfg"),
position=Position(line=13, character=13),
Expand Down Expand Up @@ -119,6 +119,43 @@ async def test_complete_section_reference(server: LanguageServer):
option4 = value4
```""")

# complete section names in [se|]
params = CompletionParams(
text_document=TextDocumentIdentifier(uri="file:///completions/sections.cfg"),
position=Position(line=15, character=1),
context=context,
)
completions = await lsp_completion(server, params)
assert completions is not None
assert sorted(
[
(c.text_edit.range, c.text_edit.new_text)
for c in completions
if isinstance(c.text_edit, TextEdit)
]
) == [
(
Range(start=Position(line=15, character=1), end=Position(line=15, character=1)),
"buildout",
),
(
Range(start=Position(line=15, character=1), end=Position(line=15, character=1)),
"section1",
),
(
Range(start=Position(line=15, character=1), end=Position(line=15, character=1)),
"section2",
),
(
Range(start=Position(line=15, character=1), end=Position(line=15, character=1)),
"section3",
),
(
Range(start=Position(line=15, character=1), end=Position(line=15, character=1)),
"xsection4",
),
]

# test completions from various positions where all sections are suggested
for completion_position, textEditRange in (
(
Expand Down

0 comments on commit 12b5b81

Please sign in to comment.