Skip to content

Commit

Permalink
Fixed some issues with snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Apr 26, 2024
1 parent a345882 commit b463423
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
Binary file modified config/absytree_config_wasm.wasm
Binary file not shown.
7 changes: 0 additions & 7 deletions config/languages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ setOption "editor.text.lsp.zig.path", "zls"
setOption "editor.text.lsp.rust.path", "rust-analyzer"
setOption "editor.text.lsp.nim.path", "nimlangserver"
setOption "editor.text.lsp.nim.args", %*["--stdio"]
setOption "editor.text.treesitter.rust.dll", "D:/dev/Nim/nimtreesitter/treesitter_rust/treesitter_rust/rust.dll"
setOption "editor.text.treesitter.zig.dll", "D:/dev/Nim/nimtreesitter/treesitter_zig/treesitter_zig/zig.dll"
setOption "editor.text.treesitter.javascript.dll", "D:/dev/Nim/nimtreesitter/treesitter_javascript/treesitter_javascript/javascript.dll"
setOption "editor.text.treesitter.nim.dll", "D:/dev/Nim/nimtreesitter/treesitter_nim/treesitter_nim/nim.dll"
setOption "editor.text.treesitter.python.dll", "D:/dev/Nim/nimtreesitter/treesitter_python/treesitter_python/python.dll"
setOption "editor.text.treesitter.cpp.dll", "D:/dev/Nim/nimtreesitter/treesitter_cpp/treesitter_cpp/cpp.dll"
setOption "editor.text.treesitter.c.dll", "D:/dev/Nim/nimtreesitter/treesitter_c/treesitter_c/c.dll"

setOption "editor.text.language.nim", %*{
"tabWidth": 2,
Expand Down
10 changes: 10 additions & 0 deletions src/text/snippet.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import npeg
import npeg/codegen
import scripting_api
import regex
import misc/custom_logger

logCategory "snippet"

type
TokenKind* = enum Text, Nested, Variable, Choice, Format
Expand Down Expand Up @@ -39,6 +42,13 @@ type SnippetData* = object
indent: int
location: Cursor

proc offset*(data: var SnippetData, selection: Selection) =
for tabStops in data.tabStops.mvalues:
for tabStop in tabStops.mitems:
let uiae = tabStop
tabStop.first = tabStop.first.add(selection)
tabStop.last = tabStop.last.add(selection)

proc updateSnippetData(data: var SnippetData, tokens: openArray[Token], variables: Table[string, string]) =
for token in tokens:
case token.kind:
Expand Down
28 changes: 20 additions & 8 deletions src/text/text_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,9 @@ proc applySelectedCompletion*(self: TextDocumentEditor) {.expose("editor.text").
else:
editSelection = self.getCompletionSelectionAt(cursor)

var editSelections: seq[Selection] = @[]
var insertTexts: seq[string] = @[]

var snippetData = SnippetData.none
if insertTextFormat == InsertTextFormat.Snippet:
let snippet = parseSnippet(insertText)
Expand All @@ -2084,17 +2087,26 @@ proc applySelectedCompletion*(self: TextDocumentEditor) {.expose("editor.text").
insertText = data.text
snippetData = data.some

self.selections = self.document.edit([editSelection], self.selections, [insertText]).mapIt(it.last.toSelection)

self.currentSnippetData = snippetData
self.selectNextTabStop()

self.addNextCheckpoint("insert")
for edit in com.additionalTextEdits:
let runeSelection = ((edit.`range`.start.line, edit.`range`.start.character.RuneIndex), (edit.`range`.`end`.line, edit.`range`.`end`.character.RuneIndex))
let selection = self.document.runeSelectionToSelection(runeSelection)
debugf"additional text edit: {selection} -> '{edit.newText}'"
discard self.document.edit([selection], self.selections, [edit.newText]).mapIt(it.last.toSelection)
editSelections.add selection
insertTexts.add edit.newText

let changedSelection = selection.getChangedSelection(edit.newText)

if snippetData.isSome:
snippetData.get.offset(changedSelection)

editSelections.add editSelection
insertTexts.add insertText

self.addNextCheckpoint("insert")
let newSelections = self.document.edit(editSelections, self.selections, insertTexts)
self.selection = newSelections[newSelections.high]

self.currentSnippetData = snippetData
self.selectNextTabStop()

self.hideCompletions()

Expand Down

0 comments on commit b463423

Please sign in to comment.