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

Fsharp #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
61 changes: 59 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ function handleInitialized(buf, filetype)
send("textDocument/didOpen",
fmt.Sprintf('{"textDocument": {"uri": "%s", "languageId": "%s", "version": 1, "text": "%s"}}', uri, filetype,
content), true)

if filetype == "fsharp" then
local directory = rootUri:gsub("file://", "")
send("fsharp/workspacePeek", fmt.Sprintf('{"directory": "%s", "deep": 4, "excludedDirs": [ ".git", "paket-files", ".fable", "packages", "node_modules" ], "$type": "WorkspacePeekRequest"}', directory), false)
end
end

function onBufferOpen(buf)
Expand Down Expand Up @@ -404,6 +409,8 @@ function onStdout(filetype)

micro.Log(filetype .. " <<< " .. (data.method or 'no method'))

micro.Log("DATA", data)

if data.method == "workspace/configuration" then
-- actually needs to respond with the same ID as the received JSON
local message = fmt.Sprintf('{"jsonrpc": "2.0", "id": %.0f, "result": [{"enable": true}]}', data.id)
Expand Down Expand Up @@ -433,17 +440,47 @@ function onStdout(filetype)
end
elseif currentAction[filetype] and currentAction[filetype].method and not data.method and currentAction[filetype].response and data.jsonrpc then -- react to custom action event
local bp = micro.CurPane()
micro.Log("Received message for ", filetype, data)
micro.Log("Received message for", filetype, currentAction[filetype].method, ":" ,data)
currentAction[filetype].response(bp, data)
currentAction[filetype] = {}
elseif data.method == "window/showMessage" or data.method == "window\\/showMessage" then

if data["error"] ~= nil then
local error = data.error
micro.InfoBar():Message(error.code, " ", error.message)
end

elseif data.method == "window/showMessage" or data.method == "window\\/showMessage" or data.method == "window/showMessageRequest" then
if filetype == micro.CurPane().Buf:FileType() then
micro.InfoBar():Message(data.params.message)
else
micro.Log(filetype .. " message " .. data.params.message)
end
elseif data.method == "window/logMessage" or data.method == "window\\/logMessage" then
micro.Log(data.params.message)
elseif data.method == "fsharp/notifyWorkspace" or data.method == "fsharp/testDetected" or data.method == "fsharp/fileParsed" or data.method == "fsharp/documentAnalyzed" then
-- ignore for now (possibly move to the default config if no use for those)
micro.Log("Done nothing for", data.method)
elseif filetype == "fsharp" and data["result"] ~= nil and data.result["content"] ~= nil then
local body = data.result.content:gsub("\\\"", "\"")
local json = json.parse(body, 1, '}')

if json.Kind == "workspacePeek" then
if next(json.Data.Found) ~= nil then
-- as for now we always just pick whatever first is returned
local found = json.Data.Found[1]
local send = withSend(filetype)
local projects = {}

if found.Type == "solution" then getProjects(found, projects)
elseif found.Type == "directory" then projects = found.Data.Fsprojs
end

-- micro.Log("PROJS:", projects)
local documents = next(projects) == nil and "" or fmt.Sprintf('{"Uri": "%s"}', projects[1])
local message = fmt.Sprintf('{"TextDocuments":[%s]}', documents)
send("fsharp/workspaceLoad", message, false)
end
end
elseif message:starts("Content-Length:") then
if message:find('"') and not message:find('"result":null') then
micro.Log("Unhandled message 1", filetype, message)
Expand Down Expand Up @@ -476,6 +513,26 @@ function onExit(filetype)
end
end

function getProjects(data, out)
if next(data) == nil then
return {}
else
local key, value = next(data)
if type(value) == "table" then getProjects(value, out) end
-- micro.Log("+", key)
local rest = data
rest[key] = nil
-- micro.Log("______")
for k, v in pairs(rest) do
-- micro.Log(k, v)
if k == "Name" and string.find(v, "\.fsproj$") ~= nil then
table.insert(out, v)
end
if type(v) == "table" then getProjects(v, out) end
end
end
end

-- the actual hover action request and response
-- the hoverActionResponse is hooked up in
function hoverAction(bp)
Expand Down