This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
/
scope-descriptor.js
80 lines (74 loc) · 2.24 KB
/
scope-descriptor.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
// Extended: Wraps an {Array} of `String`s. The Array describes a path from the
// root of the syntax tree to a token including _all_ scope names for the entire
// path.
//
// Methods that take a `ScopeDescriptor` will also accept an {Array} of {String}
// scope names e.g. `['.source.js']`.
//
// You can use `ScopeDescriptor`s to get language-specific config settings via
// {Config::get}.
//
// You should not need to create a `ScopeDescriptor` directly.
//
// * {TextEditor::getRootScopeDescriptor} to get the language's descriptor.
// * {TextEditor::scopeDescriptorForBufferPosition} to get the descriptor at a
// specific position in the buffer.
// * {Cursor::getScopeDescriptor} to get a cursor's descriptor based on position.
//
// See the [scopes and scope descriptor guide](http://flight-manual.atom.io/behind-atom/sections/scoped-settings-scopes-and-scope-descriptors/)
// for more information.
module.exports =
class ScopeDescriptor {
static fromObject (scopes) {
if (scopes instanceof ScopeDescriptor) {
return scopes
} else {
return new ScopeDescriptor({scopes})
}
}
/*
Section: Construction and Destruction
*/
// Public: Create a {ScopeDescriptor} object.
//
// * `object` {Object}
// * `scopes` {Array} of {String}s
constructor ({scopes}) {
this.scopes = scopes
}
// Public: Returns an {Array} of {String}s
getScopesArray () {
return this.scopes
}
getScopeChain () {
// For backward compatibility, prefix TextMate-style scope names with
// leading dots (e.g. 'source.js' -> '.source.js').
if (this.scopes[0] != null && this.scopes[0].includes('.')) {
let result = ''
for (let i = 0; i < this.scopes.length; i++) {
const scope = this.scopes[i]
if (i > 0) { result += ' ' }
if (scope[0] !== '.') { result += '.' }
result += scope
}
return result
} else {
return this.scopes.join(' ')
}
}
toString () {
return this.getScopeChain()
}
isEqual (other) {
if (this.scopes.length !== other.scopes.length) {
return false
}
for (let i = 0; i < this.scopes.length; i++) {
const scope = this.scopes[i]
if (scope !== other.scopes[i]) {
return false
}
}
return true
}
}