ScriptEditorServiceAPI - AutoComplete Help

  1. What do you want to achieve? Keep it simple and clear!
    I’m doing a plugin with various auto completions, and self-made. I’m currently trying to fix a bug I’m getting when it auto-completes.

  2. What is the issue? Include screenshots / videos if possible!
    When it auto-completes, the text you writed will remain there. Look it!

    video

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve already tried CompletionItemKind… Not working!

This is my current RequestFinished function:

function completionRequestFinished(Req : Request, Resp : Response): Response
	for i, v in pairs(require(script.Parent.Modules.Completions)) do
		local ScriptDocument = Req.textDocument.document
		if ScriptDocument:IsCommandBar() or not (ScriptDocument:IsCommandBar() ~= nil) then return Resp end
		local StartLine, StartCharacter = ScriptDocument:GetSelectionStart()
		local EndLine, EndCharacter = ScriptDocument:GetSelectionEnd()
		
		table.insert(Resp.items, {
			label = i, 
			codeSample = v.codeSample,
			detail = v.Description,
			kind = v.CompletionItemKind,
			textEdit = {
				newText = v.Code,
				replace = {
					start = {line = StartLine, character = StartCharacter},
					["end"] = {line = EndLine, character = EndCharacter}
				}
			}
		})
	end
	
	return Resp
end

If it’s needed I can give more part of the script.

Hi, I know this post is quite old now but after a few minutes of trying different things I seem to have figured out a fix that seems to work.

I subtract the number of characters typed from the StartCharacter value passed into the response.

I hope this helps :grin:

The “key” variable I reference is your “i” in the for loop

local ScriptDocument = Req.textDocument.document
local StartLine, StartPos, EndLine, EndPos = ScriptDocument:GetSelection()
local line = ScriptDocument:GetLine(StartLine):sub(1, StartPos - 1);

for i = 1, math.min(#key, #line) do
	local index = #line - i + 1;
	local char = line:sub(index, index):lower():byte();
	if char < ("a"):byte() or char > ("z"):byte() then
		break;
	end
	StartPos -= 1;
end

table.insert(Resp.items, {
	label = key, 
	codeSample = v.codeSample,
	detail = v.Description,
	kind = v.CompletionItemKind,
	textEdit = {
		newText = "it worked",
		replace = {
			["start"] = {
				line = StartLine,
				character = StartPos
			},
			["end"] = {
				line = EndLine,
				character = EndPos
			}
		}
	}
});

As a little bonus, if you want the autocomplete to not disappear after you type the full keyword then just append a space to the and of the keyword in the response like so:

table.insert(Resp.items, {
	label = keyword .. " ",
    ...
2 Likes

Yeah, the post is quite old, but thanks for helping me out! I’ll update this in a few mins. Thanks! :heart_hands:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.