Skip to content

Commit

Permalink
Merge pull request #357 from mstreeter10/ulsp
Browse files Browse the repository at this point in the history
ulsp: Add dot and dual colon completion support
  • Loading branch information
Jafaral authored Jan 25, 2024
2 parents 1496f1c + 9461c96 commit 97f2113
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 15 deletions.
12 changes: 9 additions & 3 deletions uni/ulsp/completion.icn
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ class CompletionHandler(
case _context of {
"comment" : {
results_table["items"] := []
}
}
"string" : {
results_table["items"] := []
}
"object" : {
workspace.buildObjectCompletionItems(results_table, context.objectName)
}
}
"packdualcolon" : {
workspace.buildPackageConstructorItems(results_table, context.packageName)
}
"package" : {
addPackageCompletionItems(results_table)
}
Expand All @@ -41,8 +47,8 @@ class CompletionHandler(
}
default : {
buildDefaultCompletionItems(results_table, workspace)
}
}
}

results := tojson(results_table)
return results
Expand Down
23 changes: 17 additions & 6 deletions uni/ulsp/server.icn
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,23 @@ class Server(
# Follow the LSP specifications on how to add a particular capability to your initialize string.

method initialize(request_id)
local results, res
results := "{\"capabilities\":{\"completionProvider\":{\"resolveProvider\":true},\"textDocumentSync\":1,\"signatureHelpProvider\":{\"contextSupport\":true,\"triggerCharacters\":\"(\"},\"hoverProvider\":true}}"

res := build_response(request_id, results)
write(res)
writes(sock, res)
local capabilitiesTable, result

capabilitiesTable := ["capabilities": [
"completionProvider": [
"triggerCharacters": [".", ":"]
];
"textDocumentSync": 1;
"signatureHelpProvider": [
"contextSupport": "__true__";
"triggerCharacters": ["("]
];
"hoverProvider": "__true__"
]]

result := build_response(request_id, tojson(capabilitiesTable))
write(result)
writes(sock, result)
end

########################################################
Expand Down
60 changes: 54 additions & 6 deletions uni/ulsp/workspace.icn
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,17 @@ class Workspace(
}
end

method buildPackageConstructorItems(results_table, packageName)
if member(lsp_database.package_db, packageName) then {
every _file := key(lsp_database.package_db[packageName]["files"]) do {
every _class := key(lsp_database.package_db[packageName]["files"][_file]["classes"]) do {
_constructor := lsp_database.package_db[packageName]["files"][_file]["classes"][_class]["constructor"]
put(results_table["items"], table("label", _constructor["name"], "kind", 4))
}
}
}
end

method getPackages()
return lsp_database.package_db
end
Expand Down Expand Up @@ -549,7 +560,7 @@ class Workspace(

end

class Context(uniAll, uri, line, lineNum, charNum, objectName, methodName, contextCase)
class Context(uniAll, uri, line, lineNum, charNum, objectName, methodName, contextCase, packageName)
method getDesiredLine(lineNum)
line := uniAll.getUniFileLine(uri, lineNum)
end
Expand Down Expand Up @@ -598,7 +609,7 @@ class Context(uniAll, uri, line, lineNum, charNum, objectName, methodName, conte
end

method findContext()
local objectName, single_quote, double_quote, ch
local objectName, single_quote, double_quote, ch, backslash_count, temppos, c

#-----------------------------------------------#
#----------- Case "start of file" --------------#
Expand Down Expand Up @@ -627,17 +638,54 @@ class Context(uniAll, uri, line, lineNum, charNum, objectName, methodName, conte
}

#-----------------------------------------------#
#----------- Case "inside comment" -------------#
#------- Case "inside comment or string" -------#
#-----------------------------------------------#

line ? {
single_quote := 0
double_quote := 0
backslash_count := 0
while (&pos < charNum) do {
ch := move(1) | break
if ch == "\'" then single_quote +:= 1
if ch == "\"" then double_quote +:= 1
if (ch == "#") & ((single_quote % 2) = 0 & (double_quote % 2) = 0) then return "comment"
if ch == "\'" then {
temppos := &pos
move(-1)
while move(-1) == "\\" do backslash_count +:= 1
if (backslash_count % 2) = 0 then single_quote +:= 1
backslash_count := 0
&pos := temppos
}
if ch == "\"" then {
temppos := &pos
move(-1)
while move(-1) == "\\" do backslash_count +:= 1
if (backslash_count % 2) = 0 then double_quote +:= 1
backslash_count := 0
&pos := temppos
}
if (ch == "#") & ((single_quote % 2) = 0) & ((double_quote % 2) = 0) then return "comment"
}
if ((single_quote % 2) ~= 0 | (double_quote % 2) ~= 0) then return "string"
}

#-----------------------------------------------#
#--------- Case "package dual colon" -----------#
#-----------------------------------------------#

line ? {
tab(charNum)
move(-2)
if move() == ":" & move() == ":" then {
move(-2)
temppos := &pos
while c := move(-1) do {
if (c ** identifiers) ~== c then {
break
}
}
if &pos ~= 1 then move(1)
packageName := tab(temppos)
return "packdualcolon"
}
}

Expand Down

0 comments on commit 97f2113

Please sign in to comment.