@@ -9,40 +9,51 @@ export const wordRegex = /[\u00A0-\uFFFF\w_!´\.]*@?[\u00A0-\uFFFF\w_!´]+/
9
9
* function `fn` with arguments `word` and `range`.
10
10
*/
11
11
export function withWord ( editor , fn ) {
12
- const { word, range } = getWord ( editor )
12
+ const { word, range } = getWordAndRange ( editor )
13
13
// If we only find numbers or nothing, return prematurely
14
14
if ( ! isValidWordToInspect ( word ) ) return
15
15
fn ( word , range )
16
16
}
17
17
18
18
/**
19
- * Gets the word and its range in the `editor`
19
+ * Returns the word and its range in the `editor`.
20
20
*
21
- * `bufferPosition` {Point}: If given returns the word at the `bufferPosition`, returns the word at the current cursor otherwise.
21
+ * `options`
22
+ * - `bufferPosition` {Point}: If given returns the word at the `bufferPosition`, returns the word at the current cursor otherwise.
23
+ * - `wordRegex` {RegExp} : A RegExp indicating what constitutes a “word” (default: `wordRegex`).
22
24
*/
23
- export function getWord ( editor , bufferPosition ) {
25
+ export function getWordAndRange ( editor , options = {
26
+ bufferPosition : undefined ,
27
+ wordRegex : wordRegex
28
+ } ) {
24
29
// @TODO ?:
25
30
// The following lines are kinda iffy: The regex may or may not be well chosen
26
31
// and it duplicates the efforts from atom-language-julia.
27
32
// It might be better to select the current word via finding the smallest <span>
28
33
// containing the bufferPosition/cursor which also has `function` or `macro` as its class.
29
- const range = bufferPosition ?
30
- getWordRangeAtBufferPosition ( editor , bufferPosition ) :
31
- editor . getLastCursor ( ) . getCurrentWordBufferRange ( { wordRegex } )
34
+ const bufferPosition = options . bufferPosition ?
35
+ options . bufferPosition :
36
+ editor . getLastCursor ( ) . getBufferPosition ( )
37
+ const range = getWordRangeAtBufferPosition ( editor , bufferPosition , {
38
+ wordRegex : options . wordRegex ? options . wordRegex : wordRegex
39
+ } )
32
40
const word = editor . getTextInBufferRange ( range )
33
41
return { word, range }
34
42
}
35
43
36
44
/**
37
- * get the word under `bufferPosition` in `editor`
38
- * adapted from https://github.com/atom/atom/blob/v1.38.2/src/cursor.js#L606-L616
45
+ * Returns the range of a word containing the `bufferPosition` in `editor`.
39
46
*
40
- * - optionalWordRegex: if not given, the toplevel `wordRegex` would be used
47
+ * `options`
48
+ * - `wordRegex` {RegExp}: A RegExp indicating what constitutes a “word” (default: `wordRegex`).
41
49
*/
42
- export function getWordRangeAtBufferPosition ( editor , bufferPosition , optionalWordRegex ) {
50
+ export function getWordRangeAtBufferPosition ( editor , bufferPosition , options = {
51
+ wordRegex : wordRegex
52
+ } ) {
53
+ // adapted from https://github.com/atom/atom/blob/v1.38.2/src/cursor.js#L606-L616
43
54
const { row, column } = bufferPosition
44
55
const ranges = editor . getBuffer ( ) . findAllInRangeSync (
45
- optionalWordRegex || wordRegex ,
56
+ options . wordRegex ? options . wordRegex : wordRegex ,
46
57
new Range ( new Point ( row , 0 ) , new Point ( row , Infinity ) )
47
58
)
48
59
const range = ranges . find ( range =>
@@ -51,6 +62,35 @@ export function getWordRangeAtBufferPosition(editor, bufferPosition, optionalWor
51
62
return range ? Range . fromObject ( range ) : new Range ( bufferPosition , bufferPosition )
52
63
}
53
64
65
+ /**
66
+ * Examples: `|` represents `bufferPosition`:
67
+ * - `"he|ad.word.foot"` => `Range` of `"head"`
68
+ * - `"head|.word.foot"` => `Range` of `"head"`
69
+ * - `"head.|word.foot"` => `Range` of `"head.word"`
70
+ * - `"head.word.fo|ot"` => `Range` of `"head.word.field"`
71
+ */
72
+ export function getWordRangeWithoutTrailingDots ( word , range , bufferPosition ) {
73
+ const { start } = range
74
+ const { column : startColumn } = start
75
+ const { row : endRow } = range . end
76
+ let endColumn = startColumn
77
+
78
+ const { column } = bufferPosition
79
+
80
+ const elements = word . split ( '.' )
81
+ for ( const element of elements ) {
82
+ endColumn += element . length
83
+ if ( column <= endColumn ) {
84
+ break
85
+ } else {
86
+ endColumn += 1
87
+ }
88
+ }
89
+
90
+ const end = new Point ( endRow , endColumn )
91
+ return new Range ( start , end )
92
+ }
93
+
54
94
/**
55
95
* Returns `true` if `word` is valid word to be inspected.
56
96
*/
0 commit comments