From 1ed31ef52b8af8b83569a6d64ec9052a3f64b736 Mon Sep 17 00:00:00 2001 From: Lukasz Papierkowski Date: Thu, 25 Jul 2024 13:18:44 +0200 Subject: [PATCH] AI Assistant: Add automatic suggestions Change-Id: I5d3297a96a215ed07dfb9d2afa85c9bb081c5cf2 Reviewed-by: Marcus Tillmanns --- .../lua-plugins/ai_asistant/ai_asistant.lua | 3 +++ .../lua-plugins/ai_asistant/init.lua | 20 ++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/share/qtcreator/lua-plugins/ai_asistant/ai_asistant.lua b/share/qtcreator/lua-plugins/ai_asistant/ai_asistant.lua index f50d3e2aea5..0545f68dcd3 100644 --- a/share/qtcreator/lua-plugins/ai_asistant/ai_asistant.lua +++ b/share/qtcreator/lua-plugins/ai_asistant/ai_asistant.lua @@ -23,6 +23,9 @@ It will try to install it if it is not found. contentsChanged = function(document, position, charsRemoved, charsAdded) require 'init'.Hooks.onDocumentContentsChanged(document, position, charsRemoved, charsAdded) end, + currentChanged = function(editor) + require 'init'.Hooks.onCurrentChanged(editor) + end, } } }, diff --git a/share/qtcreator/lua-plugins/ai_asistant/init.lua b/share/qtcreator/lua-plugins/ai_asistant/init.lua index 404dd8efb2d..62b1d267ced 100644 --- a/share/qtcreator/lua-plugins/ai_asistant/init.lua +++ b/share/qtcreator/lua-plugins/ai_asistant/init.lua @@ -8,6 +8,7 @@ local a = require('async') local TextEditor = require('TextEditor') Hooks = {} +AutoSuggestionDelay = 500 local function collectSuggestions(responseTable) local suggestions = {} @@ -209,19 +210,24 @@ local function requestSuggestions() sendRequest(request_msg) end -local function requstSuggestionsSafe() +local function requestSuggestionsSafe() local ok, err = pcall(requestSuggestions) if not ok then print("echo Error fetching: " .. err) end end -function Hooks.onDocumentContentsChanged(document, position, charsRemoved, charsAdded) - print("onDocumentcontentsChanged() called, position, charsRemoved, charsAdded:", position, charsRemoved, charsAdded) +AutoSuggestionTimer = Utils.Timer.create(AutoSuggestionDelay, true, + function() a.sync(requestSuggestionsSafe)() end) - Position = position - local line, char = document:blockAndColumn(position) - print("Line: ", line, "Char: ", char) +function Hooks.onDocumentContentsChanged(document, position, charsRemoved, charsAdded) + print("onDocumentContentsChanged() called, position, charsRemoved, charsAdded:", position, charsRemoved, charsAdded) + AutoSuggestionTimer:start() +end + +function Hooks.onCurrentChanged(editor) + print("onCurrentChanged() called") + AutoSuggestionTimer:stop() end local function setup(parameters) @@ -231,7 +237,7 @@ local function setup(parameters) Action = require("Action") Action.create("Trigger.suggestions", { text = "Trigger AI suggestions", - onTrigger = function() a.sync(requstSuggestionsSafe)() end, + onTrigger = function() a.sync(requestSuggestionsSafe)() end, defaultKeySequences = { "Meta+Shift+A", "Ctrl+Shift+Alt+A" }, }) end