1
+ CSON = require ' season'
2
+
1
3
module .exports =
2
4
selector : ' .text.html.basic, .source.gfm'
3
5
4
6
# Public: Gets the current set of suggestions.
5
7
#
6
8
# * `request` Relevant editor state to inform the list of suggestions returned. It consists of:
7
9
# * `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.
9
11
# * `scopeDescriptor` The [scope descriptor](https://atom.io/docs/latest/behind-atom-scoped-settings-scopes-and-scope-descriptors#scope-descriptors)
10
12
# for the current cursor position.
11
13
# * `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.
12
17
getSuggestions : ({editor, bufferPosition}) ->
13
18
prefix = @ getPrefix (editor, bufferPosition)
19
+ return [] unless prefix .length > 0
14
20
15
- if prefix .length > 0
16
- new Promise (resolve ) =>
17
- resolve (@ buildSuggestions (prefix))
18
- else
19
- []
21
+ new Promise (resolve ) =>
22
+ resolve (@ buildSuggestions (prefix))
20
23
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.
21
45
buildSuggestions : (prefix ) ->
22
46
suggestions = []
23
47
for completion in @completions
@@ -26,21 +50,13 @@ module.exports =
26
50
27
51
suggestions
28
52
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.
38
59
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
43
60
line = editor .getTextInRange ([[bufferPosition .row , 0 ], bufferPosition])
44
61
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 ' '
0 commit comments