This repository was archived by the owner on Feb 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstantProvider.coffee
131 lines (95 loc) · 4.5 KB
/
ConstantProvider.coffee
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
130
131
{Point, Range} = require 'atom'
AbstractProvider = require './AbstractProvider'
module.exports =
##*
# Provides code navigation for global constants.
##
class ConstantProvider extends AbstractProvider
###*
* @inheritdoc
###
canProvideForBufferPosition: (editor, bufferPosition) ->
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, bufferPosition)
return false if 'php' not in classList
return true if 'constant' in classList and 'class' not in classList
return true if 'namespace' in classList and 'constant' in @scopeDescriptorHelper.getClassListFollowingBufferPosition(editor, bufferPosition)
if 'punctuation' in classList
originalClassList = classList
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, bufferPosition, 2)
if 'namespace' in classList
climbCount = 1
if 'punctuation' in originalClassList
climbCount = 2
return true if 'constant' in @scopeDescriptorHelper.getClassListFollowingBufferPosition(editor, bufferPosition, climbCount)
return false
###*
* @inheritdoc
###
getRangeForBufferPosition: (editor, bufferPosition) ->
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, bufferPosition)
originalClassList = classList
if 'punctuation' in classList
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, bufferPosition, 2)
range = @scopeDescriptorHelper.getBufferRangeForClassListAtPosition(editor, classList, bufferPosition, 0)
if 'constant' in classList
prefixRange = new Range(
new Point(range.start.row, range.start.column - 2),
new Point(range.start.row, range.start.column - 0)
)
# Expand the range to include the namespace prefix, if present. We use two positions before the constant as
# the slash itself sometimes has a "punctuation" class instead of a "namespace" class or, if it is alone, no
# class at all.
prefixText = editor.getTextInBufferRange(prefixRange)
if prefixText.endsWith("\\")
prefixClassList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, prefixRange.start)
if "namespace" in prefixClassList
namespaceRange = @scopeDescriptorHelper.getBufferRangeForClassListAtPosition(editor, prefixClassList, prefixRange.start, 0)
else
namespaceRange = range
namespaceRange.start.column--
range = namespaceRange.union(range)
else if 'namespace' in classList
climbCount = 1
if 'punctuation' in originalClassList
climbCount = 2
suffixClassList = @scopeDescriptorHelper.getClassListFollowingBufferPosition(editor, bufferPosition, climbCount)
# Expand the range to include the constant name, if present.
if 'constant' in suffixClassList
constantRange = @scopeDescriptorHelper.getBufferRangeForClassListAtPosition(editor, suffixClassList, new Point(range.end.row, range.end.column + 1))
range = range.union(constantRange)
else
return null
return range
###*
* @param {String} text
*
* @return {Promise}
###
getInfoFor: (text) ->
successHandler = (constants) =>
if text?[0] != '\\'
text = '\\' + text
return null unless constants and text of constants
return null unless constants[text].filename
return constants[text]
failureHandler = () ->
# Do nothing.
return @service.getGlobalConstants().then(successHandler, failureHandler)
###*
* @inheritdoc
###
handleSpecificNavigation: (editor, range, text) ->
failureHandler = () ->
# Do nothing.
resolveTypeHandler = (type) =>
successHandler = (info) =>
return if not info?
atom.workspace.open(info.filename, {
initialLine : (info.startLine - 1),
searchAllPanes : true
})
return @getInfoFor(type).then(successHandler, failureHandler)
@service.resolveType(editor.getPath(), range.start.row + 1, text, 'constant').then(
resolveTypeHandler,
failureHandler
)