Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ulsp: Add dot and dual colon completion support #357

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same as

if  = "::" then {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is a bit different. Since = will only move the scan position if there is a match.

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
Loading