Files
qt-creator/share/qtcreator/lua-plugins/ai_asistant/init.lua
Lukasz Papierkowski 3aa1111560 QtAIAssistant: Document notifications
Change-Id: I75af8a63268e9a53b4116aa2b41d3a35c95628f1
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
2024-07-11 21:37:03 +00:00

142 lines
3.3 KiB
Lua

-- Copyright (C) 2024 The Qt Company Ltd.
-- SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
local LSP = require('LSP')
local Utils = require('Utils')
local S = require('Settings')
local Gui = require('Gui')
local a = require('async')
local Documents = require('Documents')
local function createCommand()
local cmd = { Settings.binary.expandedValue:nativePath() }
return cmd
end
local function installOrUpdateServer()
-- TODO: Download/Update/Install
end
IsTryingToInstall = false
local function setupClient()
Client = LSP.Client.create({
name = 'AI Assistant Server',
cmd = createCommand,
transport = 'stdio',
languageFilter = {
patterns = { '*.*' },
mimeTypes = { 'text/plain' }
},
settings = Settings,
startBehavior = "AlwaysOn",
onStartFailed = function()
a.sync(function()
if IsTryingToInstall == true then
return
end
IsTryingToInstall = true
installOrUpdateServer()
IsTryingToInstall = false
end)()
end
})
end
local function using(tbl)
local result = _G
for k, v in pairs(tbl) do result[k] = v end
return result
end
local function layoutSettings()
local _ENV = using(Gui)
local layout = Form {
Settings.binary, br,
Row {
PushButton {
text = "Try to install AI Assistant Server",
onClicked = function() a.sync(installOrUpdateServer)() end,
br,
},
st
}
}
return layout
end
local function setupAspect()
Settings = S.AspectContainer.create({
autoApply = false,
layouter = layoutSettings,
})
Settings.binary = S.FilePathAspect.create({
settingsKey = "AiAssistant.Binary",
displayName = "Binary",
labelText = "Binary:",
toolTip = "The path to the AI Assistant Server",
expectedKind = S.Kind.ExistingCommand,
defaultPath = Utils.FilePath.fromUserInput("/path/to/server"),
})
return Settings
end
local function fetchSuggestions()
print("Fetching suggestions ...")
end
local function fetchSuggestionsSafe()
local ok, err = pcall(fetchSuggestions)
if not ok then
print("echo Error fetching: " .. err)
end
end
Hooks = {}
local function onDocumentChanged()
print("onDocumentChanged() called")
-- TODO:
-- All the necessary checks before sending the request
-- Create request:
-- Get/Set the current document
-- Get/Set the current cursor position
-- Get/Set the document version
-- Set response callback to handle the response
end
function Hooks.onDocumentOpened(document)
if document then
print("LuaTextDocument found: ", document)
document:setChangedCallback(onDocumentChanged)
else
error("Expected a LuaTextDocument but got nil or incorrect type")
end
end
function Hooks.onDocumentClosed(document)
print("Document closed:", document)
-- TODO: Cleanup the document references and requests
end
local function setup(parameters)
setupAspect()
setupClient()
Action = require("Action")
Action.create("Trigger.suggestions", {
text = "Trigger AI suggestions",
onTrigger = function() a.sync(fetchSuggestionsSafe)() end,
defaultKeySequences = { "Meta+Shift+A", "Ctrl+Shift+Alt+A" },
})
end
return {
setup = function() a.sync(setup)() end,
Hooks = Hooks,
}