From 2f9f7b9147327c5ee0f19bd452ea35d63ca717da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Szabo?= Date: Sun, 9 Apr 2023 17:13:50 -0300 Subject: [PATCH 1/3] Making provider work to replace ranges --- .../lib/autocomplete-manager.js | 46 +++++++++++-------- packages/autocomplete-plus/lib/main.js | 5 ++ .../spec/provider-api-spec.js | 40 +++++++++++++++- 3 files changed, 71 insertions(+), 20 deletions(-) diff --git a/packages/autocomplete-plus/lib/autocomplete-manager.js b/packages/autocomplete-plus/lib/autocomplete-manager.js index 9876d00c09..d3a79cf745 100644 --- a/packages/autocomplete-plus/lib/autocomplete-manager.js +++ b/packages/autocomplete-plus/lib/autocomplete-manager.js @@ -617,28 +617,36 @@ See https://github.com/atom/autocomplete-plus/wiki/Provider-API` if (cursors == null) { return } return this.editor.transact(() => { - for (let i = 0; i < cursors.length; i++) { - const cursor = cursors[i] - const endPosition = cursor.getBufferPosition() - const beginningPosition = [endPosition.row, endPosition.column - suggestion.replacementPrefix.length] - - if (this.editor.getTextInBufferRange([beginningPosition, endPosition]) === suggestion.replacementPrefix) { - const suffix = this.consumeSuffix ? this.getSuffix(this.editor, endPosition, suggestion) : '' - if (suffix.length) { cursor.moveRight(suffix.length) } - cursor.selection.selectLeft(suggestion.replacementPrefix.length + suffix.length) - - if ((suggestion.snippet != null) && (this.snippetsManager != null)) { - this.snippetsManager.insertSnippet(suggestion.snippet, this.editor, cursor) - } else { - cursor.selection.insertText(suggestion.text != null ? suggestion.text : suggestion.snippet, { - autoIndentNewline: this.editor.shouldAutoIndent(), - autoDecreaseIndent: this.editor.shouldAutoIndent() - }) + if(suggestion.ranges) { + for (let i = 0; i < suggestion.ranges.length; i++) { + const range = suggestion.ranges[i] + this.editor.setTextInBufferRange( + range, suggestion.text != null ? suggestion.text : suggestion.snippet + ) + } + } else { + for (let i = 0; i < cursors.length; i++) { + const cursor = cursors[i] + const endPosition = cursor.getBufferPosition() + const beginningPosition = [endPosition.row, endPosition.column - suggestion.replacementPrefix.length] + + if (this.editor.getTextInBufferRange([beginningPosition, endPosition]) === suggestion.replacementPrefix) { + const suffix = this.consumeSuffix ? this.getSuffix(this.editor, endPosition, suggestion) : '' + if (suffix.length) { cursor.moveRight(suffix.length) } + cursor.selection.selectLeft(suggestion.replacementPrefix.length + suffix.length) + + if ((suggestion.snippet != null) && (this.snippetsManager != null)) { + this.snippetsManager.insertSnippet(suggestion.snippet, this.editor, cursor) + } else { + cursor.selection.insertText(suggestion.text != null ? suggestion.text : suggestion.snippet, { + autoIndentNewline: this.editor.shouldAutoIndent(), + autoDecreaseIndent: this.editor.shouldAutoIndent() + }) + } } } } - } - ) + }) } getSuffix (editor, bufferPosition, suggestion) { diff --git a/packages/autocomplete-plus/lib/main.js b/packages/autocomplete-plus/lib/main.js index 7353e20d5c..1603ef9069 100644 --- a/packages/autocomplete-plus/lib/main.js +++ b/packages/autocomplete-plus/lib/main.js @@ -67,6 +67,11 @@ module.exports = { return this.consumeProvider(providers, 4) }, + // 5.0.0 API – Make autocomplete play nicer with LSP using the same API + consumeProvider_5 (providers) { + return this.consumeProvider(providers, 5) + }, + consumeProvider (providers, apiVersion = 3) { if (!providers) { return diff --git a/packages/autocomplete-plus/spec/provider-api-spec.js b/packages/autocomplete-plus/spec/provider-api-spec.js index 1cd506b5bb..f1c04287eb 100644 --- a/packages/autocomplete-plus/spec/provider-api-spec.js +++ b/packages/autocomplete-plus/spec/provider-api-spec.js @@ -1,7 +1,11 @@ 'use babel' /* eslint-env jasmine */ -import {waitForAutocomplete, triggerAutocompletion, conditionPromise} from './spec-helper' +import { + waitForAutocomplete, + triggerAutocompletion, + conditionPromise, + waitForAutocompleteToDisappear} from './spec-helper' import path from 'path' describe('Provider API', () => { @@ -290,4 +294,38 @@ describe('Provider API', () => { }) }) }) + + describe('Provider API v5.0.0', () => { + const getSuggestions = () => autocompleteManager.suggestionList.items.map(({text}) => ({text})) + const triggerAutocompletion = () => { + atom.commands.dispatch(atom.views.getView(editor), 'autocomplete-plus:activate') + return waitForAutocomplete(editor) + } + const confirmChoice = () => { + atom.commands.dispatch(atom.views.getView(editor), 'autocomplete-plus:confirm') + return waitForAutocompleteToDisappear(editor) + } + + beforeEach(() => editor.setText('')) + + it('replaces the right range on the editor when `range` is present', async () => { + testProvider = { + scopeSelector: '.source.js', + filterSuggestions: true, + getSuggestions (options) { + return [ + {text: 'ohai', ranges: [[[0, 0], [0, 5]]]}, + {text: 'ca.ts'}, + {text: '::dogs'} + ] + } + } + registration = atom.packages.serviceHub.provide('autocomplete.provider', '5.0.0', testProvider) + editor.insertText('hello, world\n') + await triggerAutocompletion() + await confirmChoice(0) + + expect(editor.getText()).toEqual("ohai, world\n") + }) + }) }) From 9481f0cd2c881542e19f95b9e99362704486c252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Szabo?= Date: Sun, 9 Apr 2023 18:33:07 -0300 Subject: [PATCH 2/3] Adding the new provider API to package.json --- packages/autocomplete-plus/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/autocomplete-plus/package.json b/packages/autocomplete-plus/package.json index a1d6dbcb03..107f1634d8 100644 --- a/packages/autocomplete-plus/package.json +++ b/packages/autocomplete-plus/package.json @@ -40,7 +40,8 @@ "1.1.0": "consumeProvider_1_1", "2.0.0": "consumeProvider_2", "3.0.0": "consumeProvider_3", - "4.0.0": "consumeProvider_4" + "4.0.0": "consumeProvider_4", + "5.0.0": "consumeProvider_5" } }, "snippets": { From d768ca004f9af98a2ff53560737dcccd618f7e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Szabo?= Date: Sun, 17 Sep 2023 00:46:33 -0300 Subject: [PATCH 3/3] Changelog update --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2634d6f736..f6654dacd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) ## [Unreleased] +- Added a new autocomplete API that does not uses prefixes and instead declares the range it'll replace (better LSP support) ## 1.109.0