Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making autocomplete-plus work to replace ranges #479

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 27 additions & 19 deletions packages/autocomplete-plus/lib/autocomplete-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions packages/autocomplete-plus/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/autocomplete-plus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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": {
Expand Down
40 changes: 39 additions & 1 deletion packages/autocomplete-plus/spec/provider-api-spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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")
})
})
})
Loading