@@ -9,20 +9,106 @@ function testNoTab(editor) {
9
9
}
10
10
}
11
11
12
- function getReusableString ( editor ) {
13
- let reusableString = "" ;
14
- let objSelectTextAroundCursor = editor . document . getWordRangeAtPosition ( editor . selection . active , / ( { % [ ^ % ] * % } | ^ * f e a t u r e : ' * ( [ ^ ' ] * ) ' * * $ ) / ) ;
15
- if ( objSelectTextAroundCursor ) {
16
- reusableString = editor . document . getText ( objSelectTextAroundCursor ) ;
12
+ // This regex matches liquid tags. e.g:
13
+ // - {%<something>%}
14
+ const liquidTagRegex = / { % ( [ ^ % ] ) * % } / ;
15
+
16
+ // This regex matches frontmatter feature flags definitions. e.g.:
17
+ // - feature: '<something>'
18
+ // - feature: <something>
19
+ const frontmatterFeatureFlagRegex = / ^ * f e a t u r e : ' * ( [ ^ ' ] * ) ' * * $ / ;
20
+
21
+ /* Gets the type and value of a reusable or feature flag.
22
+ Returns an array with the type and value.
23
+
24
+ The input can either be a string that is the user selection, or an editor
25
+ object that is the active editor with it's cursor position.
26
+
27
+ The return is an array with the type of docs thing being opened (i.e.
28
+ reusable or feature), and the path or name of that thing. For reusables and
29
+ variables, the path is the value of the liquid data tag (e.g.
30
+ variables.product.prodname_dotcom), for feature flags it's the name of the
31
+ flag (e.g. internal-actions).
32
+ */
33
+ function getTypeAndValue ( editorOrString ) {
34
+ let thingString = "" ;
35
+
36
+ // If the parameter is a string we can use that directly.
37
+ // Otherwise, it's an editor and we have to get the string from the editor.
38
+ if ( typeof editorOrString === 'string' ) {
39
+ //console.log('its a string');
40
+ thingString = editorOrString ;
41
+ } else {
42
+ //console.log('its an editor');
43
+ thingString = getThingString ( editorOrString ) ;
17
44
}
18
- return reusableString ;
45
+
46
+ // console.log('thingString = ' + thingString);
47
+
48
+ // Check to see if it's a reusable or a feature flag in a liquid tag.
49
+ if ( liquidTagRegex . test ( thingString ) ) {
50
+ // This variable is all the contents inside the liquid tag.
51
+ // e.g. For the liquid tag '{% data variables.product.prodname_dotcom %}', it is 'data variables.product.prodname_dotcom'
52
+ let liquidTagContents = liquidTagRegex . exec ( thingString ) [ 0 ] . trim ( ) ;
53
+
54
+ //console.log('liquidTagContents = ' + liquidTagContents);
55
+
56
+ // If it's a reusable, return the type and value.
57
+ // This regex checks for anything that matches:
58
+ // - data <something>
59
+ let reusableRegex = / d a t a ( \S * ) / ;
60
+ if ( reusableRegex . test ( liquidTagContents ) ) {
61
+ // This is just the path of the reusable, (i.e. the stuff in the liquid tag after 'data').
62
+ let reusableValue = reusableRegex . exec ( liquidTagContents ) [ 1 ] ;
63
+ return [ 'reusable' , reusableValue ] ;
64
+ }
65
+
66
+ // If it's a feature flag in a liquid tag, return the type and value.
67
+ // This regex checks for anything that matches:
68
+ // - if <something>
69
+ // - ifversion <something>
70
+ let featureFlagRegex = / (?: i f | i f v e r s i o n ) ( \S * ) / ;
71
+ if ( featureFlagRegex . test ( liquidTagContents ) ) {
72
+ // This is just the name of the feature flag, (i.e. the stuff in the after 'if' or 'ifversion').
73
+ let featureValue = featureFlagRegex . exec ( liquidTagContents ) [ 1 ] ;
74
+ return [ 'feature' , featureValue ] ;
75
+ }
76
+ }
77
+
78
+ // check for frontmatter feature flags
79
+ if ( frontmatterFeatureFlagRegex . test ( thingString ) ) {
80
+ let featureValue = frontmatterFeatureFlagRegex . exec ( thingString ) [ 1 ] ;
81
+ return [ 'feature' , featureValue ] ;
82
+ }
83
+ // If it's not a liquid or feature flag, return null.
84
+ return null ;
19
85
}
20
86
21
87
88
+ /* For an editor object that is the active editor with it's cursor position,
89
+ get's the string of a reusable frontmatter feature flag.
90
+
91
+ The return is a string that is the liquid tag or frontmatter feature flag syntax.
92
+ */
93
+ function getThingString ( editor ) {
94
+ var regexArray = [ liquidTagRegex , frontmatterFeatureFlagRegex ] ;
95
+
96
+ // Go through the array of regexes and return a string that matches.
97
+ for ( var i = 0 ; i < regexArray . length ; i ++ ) {
98
+ let objSelectTextAroundCursor = editor . document . getWordRangeAtPosition ( editor . selection . active , regexArray [ i ] ) ;
99
+ if ( objSelectTextAroundCursor ) {
100
+ return editor . document . getText ( objSelectTextAroundCursor ) . trim ( ) ;
101
+ }
102
+ }
103
+
104
+ // If we didn't find a match, return null.
105
+ return null ;
106
+ }
22
107
23
108
24
109
25
110
module . exports = {
26
111
testNoTab,
27
- getReusableString
112
+ getTypeAndValue,
113
+ getThingString
28
114
} ;
0 commit comments