forked from qt-creator/qt-creator
AI Assistant: Add automatic suggestions
Change-Id: I5d3297a96a215ed07dfb9d2afa85c9bb081c5cf2 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -23,6 +23,9 @@ It will try to install it if it is not found.
|
|||||||
contentsChanged = function(document, position, charsRemoved, charsAdded)
|
contentsChanged = function(document, position, charsRemoved, charsAdded)
|
||||||
require 'init'.Hooks.onDocumentContentsChanged(document, position, charsRemoved, charsAdded)
|
require 'init'.Hooks.onDocumentContentsChanged(document, position, charsRemoved, charsAdded)
|
||||||
end,
|
end,
|
||||||
|
currentChanged = function(editor)
|
||||||
|
require 'init'.Hooks.onCurrentChanged(editor)
|
||||||
|
end,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@@ -8,6 +8,7 @@ local a = require('async')
|
|||||||
local TextEditor = require('TextEditor')
|
local TextEditor = require('TextEditor')
|
||||||
|
|
||||||
Hooks = {}
|
Hooks = {}
|
||||||
|
AutoSuggestionDelay = 500
|
||||||
|
|
||||||
local function collectSuggestions(responseTable)
|
local function collectSuggestions(responseTable)
|
||||||
local suggestions = {}
|
local suggestions = {}
|
||||||
@@ -209,19 +210,24 @@ local function requestSuggestions()
|
|||||||
sendRequest(request_msg)
|
sendRequest(request_msg)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function requstSuggestionsSafe()
|
local function requestSuggestionsSafe()
|
||||||
local ok, err = pcall(requestSuggestions)
|
local ok, err = pcall(requestSuggestions)
|
||||||
if not ok then
|
if not ok then
|
||||||
print("echo Error fetching: " .. err)
|
print("echo Error fetching: " .. err)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Hooks.onDocumentContentsChanged(document, position, charsRemoved, charsAdded)
|
AutoSuggestionTimer = Utils.Timer.create(AutoSuggestionDelay, true,
|
||||||
print("onDocumentcontentsChanged() called, position, charsRemoved, charsAdded:", position, charsRemoved, charsAdded)
|
function() a.sync(requestSuggestionsSafe)() end)
|
||||||
|
|
||||||
Position = position
|
function Hooks.onDocumentContentsChanged(document, position, charsRemoved, charsAdded)
|
||||||
local line, char = document:blockAndColumn(position)
|
print("onDocumentContentsChanged() called, position, charsRemoved, charsAdded:", position, charsRemoved, charsAdded)
|
||||||
print("Line: ", line, "Char: ", char)
|
AutoSuggestionTimer:start()
|
||||||
|
end
|
||||||
|
|
||||||
|
function Hooks.onCurrentChanged(editor)
|
||||||
|
print("onCurrentChanged() called")
|
||||||
|
AutoSuggestionTimer:stop()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function setup(parameters)
|
local function setup(parameters)
|
||||||
@@ -231,7 +237,7 @@ local function setup(parameters)
|
|||||||
Action = require("Action")
|
Action = require("Action")
|
||||||
Action.create("Trigger.suggestions", {
|
Action.create("Trigger.suggestions", {
|
||||||
text = "Trigger AI 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" },
|
defaultKeySequences = { "Meta+Shift+A", "Ctrl+Shift+Alt+A" },
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user