-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprovider.js
129 lines (105 loc) · 3.81 KB
/
provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var fs = require( 'fs' )
var path = require( 'path' )
module.exports = {
executablePath: 'php',
// This will work on JavaScript and CoffeeScript files, but not in js comments.
selector: '.source.php',
disableForSelector: '.source.php .comment',
// This will take priority over the default provider, which has a priority of 0.
// `excludeLowerPriority` will suppress any providers with a lower priority
// i.e. The default provider will be suppressed
inclusionPriority: 1,
excludeLowerPriority: false,
loadCompletions: function() {
this.filters = []
this.actions = []
var f = this.filters
fs.readFile( path.resolve( __dirname, '..', 'hooks.json' ), (function(error, content) {
var json = JSON.parse( content )
for (var i = 0; i < json.length; i++) {
var hook = json[i]
if ( hook.type === 'action' ) {
this.actions.push( hook )
} else if ( hook.type === 'filter' ) {
this.filters.push( hook )
}
}
}).bind(this) )
},
getSuggestions: function(request) {
if ( this.isInFilter( request ) ) {
return this.filters.filter( function( filter ) {
return filter.text.indexOf( request.prefix ) > -1
}).map( this.getHookSuggestion )
}
if ( this.isInAction( request ) ) {
return this.actions.filter( function( action ) {
return action.text.indexOf( request.prefix ) > -1
}).map( this.getHookSuggestion )
}
var isInHookArgsNumber = this.isInHookArgsNumber( request )
if ( isInHookArgsNumber ) {
var hook = this.getHook( isInHookArgsNumber[2] )
if ( ! hook ) {
return []
}
return [{
text: String( hook.arguments.length )
}]
}
var isInFunctionDecleration = this.isInFunctionDecleration( request )
if ( isInFunctionDecleration ) {
var hook = this.getHook( isInFunctionDecleration[2] )
if ( ! hook ) {
return []
}
var argsString = hook.arguments.map( function( arg ) { return arg.name } ) .join( ', ' )
var stringAfterCursor = request.editor.getTextInRange([request.bufferPosition,[request.bufferPosition.row, request.bufferPosition.column+10]]).trim()
var snippet = 'function (' + ( argsString ? ' ' + argsString + ' ' : '' ) + ') {\n\t$1\n}' + ( hook.arguments.length ? ', 10, ' + hook.arguments.length + ' ' : '' );
if ( stringAfterCursor !== ')' ) {
snippet += ');'
}
return [{
snippet: snippet,
description: 'Autocomplete hook closure'
}]
}
},
getHookSuggestion: function( hook ) {
return {
text: hook.text,
description: hook.description,
type: 'keyword',
rightLabel: hook.arguments.map( function( arg ) { return arg.name } ).join( ' ' )
}
},
getHook: function( name ) {
var hooks = this.filters.filter( function( filter ) {
return filter.text === name
})
if ( hooks.length == 0 ) {
var hooks = this.actions.filter( function( action ) {
return action.text === name
})
}
if ( hooks.length ) {
return hooks[0]
}
},
isInFilter: function(request) {
var line = request.editor.buffer.lineForRow( request.bufferPosition.row ).substr( 0, request.bufferPosition.column );
return line.match( /(add|remove)_filter\([\s]*('|")[^"|']*$/ )
},
isInAction: function(request) {
var line = request.editor.buffer.lineForRow( request.bufferPosition.row ).substr( 0, request.bufferPosition.column );
return line.match( /(add|remove)_action\([\s]*('|")[^"|']*$/ )
},
isInFunctionDecleration: function(request) {
var line = request.editor.buffer.lineForRow( request.bufferPosition.row ).substr( 0, request.bufferPosition.column );
return line.match( /add_(filter|action)\([\s]*['|"]([\S]+?)['|"],[\s]*[\w]*?$/ )
},
isInHookArgsNumber: function(request) {
var line = request.editor.buffer.lineForRow( request.bufferPosition.row ).substr( 0, request.bufferPosition.column );
return line.match( /add_(filter|action)\([\s]*['|"]([\S]+?)['|"],[\s\S]*?,[\s\S]*?,\s*[\d]*$/ )
}
}