Skip to content

Commit 0ca8bd3

Browse files
committed
Move completions out to a data file
1 parent 2877238 commit 0ca8bd3

File tree

5 files changed

+83
-24
lines changed

5 files changed

+83
-24
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

data/completions.cson

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
completions:
2+
'quotation mark': '"'
3+
'ampersand': '&'
4+
'copyright symbol': '©'
5+
'apostrophe': '''
6+
'less-than sign': '<'
7+
'greater-than sign': '>'
8+
'non-breaking space': ' '
9+
'inverted exclamation mark': '¡'
10+
'cent sign': '¢'
11+
'pound sign': '£'
12+
'currency sign': '¤'
13+
'yen sign': '¥'
14+
'broken vertical bar': '¦'
15+
'section sign': '§'
16+
'diaresis': '¨'
17+
'feminine ordinal indicator': 'ª'
18+
'left-pointing double angle quotation mark': '«'
19+
'not sign': '¬'
20+
'soft hyphen': '­'
21+
'registered trademark sign': '®'
22+
'macron': '¯'
23+
'degree symbol': '°'
24+
'plus-minus sign': '±'
25+
'superscript two': '²'
26+
'superscript three': '³'
27+
'acute accent': '´'
28+
'micro sign': 'µ'
29+
'paragraph sign': '¶'
30+
'middle dot': '·'
31+
'cedilla': '¸'
32+
'superscript one': '¹'
33+
'masculine ordinal indicator': 'º'
34+
'right-pointing double angle quotation mark': '»'
35+
'vulgar fraction one quarter': '¼'
36+
'vulgar fraction one half': '½'
37+
'vulgar fraction three quarters': '¾'
38+
'inverted question mark': '¿'
39+
'multiplication sign': '&mult;'
40+
'division sign': '÷'
41+
'en dash': '–'
42+
'em dash': '—'

foo.html

-3
This file was deleted.

lib/provider.coffee

+37-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
1+
CSON = require 'season'
2+
13
module.exports =
24
selector: '.text.html.basic, .source.gfm'
35

46
# Public: Gets the current set of suggestions.
57
#
68
# * `request` Relevant editor state to inform the list of suggestions returned. It consists of:
79
# * `editor` {TextEditor} the suggestions are being requested for.
8-
# * `bufferPosition` Position of the cursor in the file.
10+
# * `bufferPosition` Position {Point} of the cursor in the file.
911
# * `scopeDescriptor` The [scope descriptor](https://atom.io/docs/latest/behind-atom-scoped-settings-scopes-and-scope-descriptors#scope-descriptors)
1012
# for the current cursor position.
1113
# * `prefix` Prefix that triggered the request for suggestions.
14+
#
15+
# Returns a {Promise} that resolves to the list of suggestions or returns an empty list
16+
# immediately.
1217
getSuggestions: ({editor, bufferPosition}) ->
1318
prefix = @getPrefix(editor, bufferPosition)
19+
return [] unless prefix.length > 0
1420

15-
if prefix.length > 0
16-
new Promise (resolve) =>
17-
resolve(@buildSuggestions(prefix))
18-
else
19-
[]
21+
new Promise (resolve) =>
22+
resolve(@buildSuggestions(prefix))
2023

24+
# Public: Loads the full set of completions.
25+
loadCompletions: ->
26+
@completions = []
27+
path = CSON.resolve("#{__dirname}/../data/completions")
28+
CSON.readFile path, (error, object) =>
29+
return if error?
30+
31+
{completions} = object
32+
@completions = for description, entity of completions
33+
{
34+
text: entity
35+
rightLabelHTML: entity
36+
description: description
37+
type: 'constant'
38+
}
39+
40+
# Private: Builds the list of suggestions from the current set of completions and the `prefix`.
41+
#
42+
# * `prefix` {String} containing the text to match and replace.
43+
#
44+
# Returns a list of applicable suggestions.
2145
buildSuggestions: (prefix) ->
2246
suggestions = []
2347
for completion in @completions
@@ -26,21 +50,13 @@ module.exports =
2650

2751
suggestions
2852

29-
loadCompletions: ->
30-
@completions = [
31-
{
32-
text: '©'
33-
rightLabelHTML: '©'
34-
description: 'copyright symbol'
35-
}
36-
]
37-
53+
# Private: Gets the appropriate prefix text to search for.
54+
#
55+
# * `editor` {TextEditor} where the autocompletion was requested.
56+
# * `bufferPosition` A {Point} or point-compatible {Array} indicating where the cursor is located.
57+
#
58+
# Returns a {String} containing the prefix text.
3859
getPrefix: (editor, bufferPosition) ->
39-
# Whatever your prefix regex might be
40-
regex = /&[A-Za-z0-9]+$/
41-
42-
# Get the text for the line up to the triggered buffer position
4360
line = editor.getTextInRange([[bufferPosition.row, 0], bufferPosition])
4461

45-
# Match the regex to the line, and return the first capture
46-
line.match(regex)?[0] or ''
62+
line.match(/&[A-Za-z0-9]+$/)?[0] or ''

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"engines": {
1616
"atom": ">=1.0.0 <2.0.0"
1717
},
18+
"dependencies": {
19+
"season": "^5.3.0"
20+
},
1821
"providedServices": {
1922
"autocomplete.provider": {
2023
"versions": {

0 commit comments

Comments
 (0)