Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Recognise minimum prefix length #100

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion lib/autocomplete-snippets.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
const {CompositeDisposable} = require('atom')

module.exports = {
subscriptions: null,
provider: null,

activate() {},
activate() {
this.subscriptions = new CompositeDisposable()
this.subscriptions.add(atom.config.observe('autocomplete-snippets.useAutocompletePlusMinimumWordLength', (value) => {
if (!this.provider) return
if (value) {
this.provider.minPrefixLength = atom.config.get('autocomplete-plus.minimumWordLength')
} else {
this.provider.minPrefixLength = atom.config.get('autocomplete-snippets.minimumWordLength')
}
}))
this.subscriptions.add(atom.config.observe('autocomplete-snippets.minimumWordLength', (value) => {
if (!this.provider || atom.config.get('autocomplete-snippets.useAutocompletePlusMinimumWordLength')) return
this.provider.minPrefixLength = value
}))
this.subscriptions.add(atom.config.observe('autocomplete-plus.minimumWordLength', (value) => {
if (!this.provider || !atom.config.get('autocomplete-snippets.useAutocompletePlusMinimumWordLength')) return
this.provider.minPrefixLength = value
}))
},

deactivate() {
this.provider = null
this.subscriptions.dispose()
},

provide() {
Expand Down
5 changes: 4 additions & 1 deletion lib/snippets-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class SnippetsProvider {
this.inclusionPriority = 1
this.suggestionPriority = 2
this.filterSuggestions = true
this.minPrefixLength = atom.config.get('autocomplete-snippets.useAutocompletePlusMinimumWordLength') ?
atom.config.get('autocomplete-plus.minimumWordLength') :
atom.config.get('autocomplete-snippets.minimumWordLength')

this.showIcon = ['Symbol', 'Subsequence'].includes(atom.config.get('autocomplete-plus.defaultProvider'))
this.snippetsSource = {
Expand All @@ -21,7 +24,7 @@ class SnippetsProvider {
}

getSuggestions({scopeDescriptor, prefix}) {
if (!(prefix != null ? prefix.length : undefined)) { return }
if (prefix == null || prefix.length < this.minPrefixLength) { return }
const scopeSnippets = this.snippetsSource.snippetsForScopes(scopeDescriptor)
return this.findSuggestionsForPrefix(scopeSnippets, prefix)
}
Expand Down
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,19 @@
"0.1.0": "consumeSnippets"
}
}
},
"configSchema": {
"useAutocompletePlusMinimumWordLength": {
"description": "When enabled, the minimum prefix length will be that set for autocomplete-plus",
"type": "boolean",
"default": true,
"order": 1
},
"minimumWordLength": {
"description": "Minimum prefix length when not using the one set for autocomplete-plus",
"type": "integer",
"default": 3,
"order": 2
}
}
}
30 changes: 30 additions & 0 deletions spec/autocomplete-snippets-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ describe('AutocompleteSnippets', () => {
let [completionDelay, editor, editorView] = []

beforeEach(() => {
atom.config.set('autocomplete-snippets.minimumWordLength', 1)
atom.config.set('autocomplete-snippets.useAutocompletePlusMinimumWordLength', false)

atom.config.set('autocomplete-plus.enableAutoActivation', true)
completionDelay = 100
atom.config.set('autocomplete-plus.autoActivationDelay', completionDelay)
Expand Down Expand Up @@ -49,6 +52,7 @@ describe('AutocompleteSnippets', () => {
advanceClock(completionDelay)
})


waitsFor('autocomplete view to appear', 1000, () => editorView.querySelector('.autocomplete-plus span.word'))

runs(() => {
Expand All @@ -75,6 +79,31 @@ describe('AutocompleteSnippets', () => {
expect(editor.getText()).toContain('} while (true)')
})
})

describe('when picking minimum prefix length', () => {
it('respects the value set for autocomplete-plus', () => {
atom.config.set('autocomplete-snippets.useAutocompletePlusMinimumWordLength', true)

const plus = atom.config.get('autocomplete-plus.minimumWordLength')

const SnippetsProvider = require('../lib/snippets-provider')
const usedVal = (new SnippetsProvider()).minPrefixLength

expect(usedVal).toEqual(plus)
})

it('can be overridden by its own setting', () => {
atom.config.set('autocomplete-snippets.useAutocompletePlusMinimumWordLength', false)
const plus = atom.config.get('autocomplete-plus.minimumWordLength')
atom.config.set('autocomplete-snippets.minimumWordLength', plus + 1)
const snippet = atom.config.get('autocomplete-snippets.minimumWordLength')

const SnippetsProvider = require('../lib/snippets-provider')
const usedVal = (new SnippetsProvider()).minPrefixLength

expect(usedVal).toEqual(snippet)
})
})
})

describe('when showing suggestions', () =>
Expand All @@ -94,6 +123,7 @@ describe('AutocompleteSnippets', () => {

const SnippetsProvider = require('../lib/snippets-provider')
const sp = new SnippetsProvider()
sp.minPrefixLength = 1
sp.setSnippetsSource({snippetsForScopes(scope) {
return snippets
}})
Expand Down