Skip to content

Commit d4358c0

Browse files
committed
simpler dot striping:
- ref: #651 (comment)
1 parent 66cfdd0 commit d4358c0

File tree

4 files changed

+68
-80
lines changed

4 files changed

+68
-80
lines changed

lib/misc/words.js

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { Point, Range } from 'atom'
44

55
export const wordRegex = /[\u00A0-\uFFFF\w_!´\.]*@?[\u00A0-\uFFFF\w_!´]+/
6-
export const wordRegexWithoutDotAccessor = /@?[\u00A0-\uFFFF\w_!´]+/
76

87
/**
98
* Takes an `editor` and gets the word at current cursor position. If that is nonempty, call
@@ -17,89 +16,79 @@ export function withWord (editor, fn) {
1716
}
1817

1918
/**
20-
* Gets the word and its range in the `editor`
19+
* Returns the word and its range in the `editor`.
2120
*
2221
* `options`
2322
* - `bufferPosition` {Point}: If given returns the word at the `bufferPosition`, returns the word at the current cursor otherwise.
24-
* - `wordRegex` {RegExp} : A RegExp indicating what constitutes a “word”. Will be used if not both `beginWordRegex and `endWordRegex` are passed. (default: `wordRegex`).
25-
* - `beginWordRegex` {RegExp} : A RegExp to find a beginning of a “word”. Should be passed with `endWordRegex`. (default: `undefined`).
26-
* - `endWordRegex` {RegExp} : A RegExp to find an end of a “word”. Should be passed with `beginWordRegex`. (default: `undefined`).
23+
* - `wordRegex` {RegExp} : A RegExp indicating what constitutes a “word” (default: `wordRegex`).
2724
*/
2825
export function getWordAndRange (editor, options = {
2926
bufferPosition: undefined,
30-
wordRegex: wordRegex,
31-
beginWordRegex: undefined,
32-
endWordRegex: undefined
27+
wordRegex: wordRegex
3328
}) {
3429
// @TODO?:
3530
// The following lines are kinda iffy: The regex may or may not be well chosen
3631
// and it duplicates the efforts from atom-language-julia.
3732
// It might be better to select the current word via finding the smallest <span>
3833
// containing the bufferPosition/cursor which also has `function` or `macro` as its class.
39-
const range = options.bufferPosition ?
40-
getWordRangeAtBufferPosition(editor, options.bufferPosition, options) :
41-
getWordRangeAtBufferPosition(editor, editor.getLastCursor().getBufferPosition(), options)
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+
})
4240
const word = editor.getTextInBufferRange(range)
4341
return { word, range }
4442
}
4543

4644
/**
47-
* get the word under `bufferPosition` in `editor`
45+
* Returns the range of a word containing the `bufferPosition` in `editor`.
4846
*
4947
* `options`
50-
* - `wordRegex` {RegExp}: A RegExp indicating what constitutes a “word”. Will be used if not both `beginWordRegex and `endWordRegex` are passed. (default: `wordRegex`).
51-
* - `beginWordRegex` {RegExp} : A RegExp to find a beginning of a “word”. Should be passed with `endWordRegex`. (default: `undefined`).
52-
* - `endWordRegex` {RegExp} : A RegExp to find an end of a “word”. Should be passed with `beginWordRegex`. (default: `undefined`).
48+
* - `wordRegex` {RegExp}: A RegExp indicating what constitutes a “word” (default: `wordRegex`).
5349
*/
54-
export function getWordRangeAtBufferPosition(editor, bufferPosition, options = {
55-
wordRegex: wordRegex,
56-
beginWordRegex: undefined,
57-
endWordRegex: undefined,
50+
export function getWordRangeAtBufferPosition (editor, bufferPosition, options = {
51+
wordRegex: wordRegex
5852
}) {
59-
if (options.beginWordRegex && options.endWordRegex) {
60-
// adapted from https://github.com/atom/atom/blob/1.42-releases/src/cursor.js#L572-L593
61-
const beginRange = new Range(new Point(bufferPosition.row, 0), bufferPosition)
62-
const beginRanges = editor.getBuffer().findAllInRangeSync(
63-
options.beginWordRegex,
64-
beginRange
65-
)
66-
let beginPosition
67-
for (const range of beginRanges) {
68-
if (bufferPosition.isLessThanOrEqual(range.start)) break
69-
if (bufferPosition.isLessThanOrEqual(range.end)) {
70-
beginPosition = Point.fromObject(range.start)
71-
}
72-
}
73-
beginPosition = beginPosition || bufferPosition
53+
// adapted from https://github.com/atom/atom/blob/v1.38.2/src/cursor.js#L606-L616
54+
const { row, column } = bufferPosition
55+
const ranges = editor.getBuffer().findAllInRangeSync(
56+
options.wordRegex ? options.wordRegex : wordRegex,
57+
new Range(new Point(row, 0), new Point(row, Infinity))
58+
)
59+
const range = ranges.find(range =>
60+
range.end.column >= column && range.start.column <= column
61+
)
62+
return range ? Range.fromObject(range) : new Range(bufferPosition, bufferPosition)
63+
}
7464

75-
// https://github.com/atom/atom/blob/1.42-releases/src/cursor.js#L605-L624
76-
const endRange = new Range(bufferPosition, new Point(bufferPosition.row, Infinity));
77-
const endRanges = editor.getBuffer().findAllInRangeSync(
78-
options.endWordRegex,
79-
endRange
80-
)
81-
let endPosition
82-
for (const range of endRanges) {
83-
if (bufferPosition.isLessThan(range.start)) break
84-
if (bufferPosition.isLessThan(range.end)) {
85-
endPosition = Point.fromObject(range.end)
86-
}
87-
}
88-
endPosition = endPosition || bufferPosition
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
8979

90-
return new Range(beginPosition, endPosition)
91-
} else {
92-
// adapted from https://github.com/atom/atom/blob/v1.38.2/src/cursor.js#L606-L616
93-
const { row, column } = bufferPosition
94-
const ranges = editor.getBuffer().findAllInRangeSync(
95-
options.wordRegex ? options.wordRegex : wordRegex,
96-
new Range(new Point(row, 0), new Point(row, Infinity))
97-
)
98-
const range = ranges.find(range =>
99-
range.end.column >= column && range.start.column <= column
100-
)
101-
return range ? Range.fromObject(range) : new Range(bufferPosition, bufferPosition)
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+
}
10288
}
89+
90+
const end = new Point(endRow, endColumn)
91+
return new Range(start, end)
10392
}
10493

10594
/**

lib/runtime/datatip.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import { client } from '../connection'
1212
import modules from './modules'
1313
import { isValidScopeToInspect } from '../misc/scopes'
1414
import {
15-
wordRegex,
16-
wordRegexWithoutDotAccessor,
1715
getWordAndRange,
16+
getWordRangeWithoutTrailingDots,
1817
isValidWordToInspect
1918
} from '../misc/words'
2019
import { getLocalContext } from '../misc/blocks'
@@ -40,11 +39,11 @@ class DatatipProvider {
4039
if (!isValidScopeToInspect(editor, bufferPosition)) return
4140

4241
// get word without trailing dot accessors at the buffer position
43-
const { range, word } = getWordAndRange(editor, {
44-
bufferPosition,
45-
beginWordRegex: wordRegex,
46-
endWordRegex: wordRegexWithoutDotAccessor,
42+
let { range, word } = getWordAndRange(editor, {
43+
bufferPosition
4744
})
45+
range = getWordRangeWithoutTrailingDots(word, range, bufferPosition)
46+
word = editor.getTextInBufferRange(range)
4847

4948
// check the validity of code to be inspected
5049
if (!(isValidWordToInspect(word))) return

lib/runtime/evaluation.coffee

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ module.exports =
8686

8787
toggleDocs: (word, range) ->
8888
{ editor, mod, edpath } = @_currentContext()
89-
{ word, range } = words.getWordAndRange(editor,
90-
beginWordRegex: words.wordRegex
91-
endWordRegex: words.wordRegexWithoutDotAccessor
92-
) unless word? and range?
89+
# get word without trailing dot accessors at the buffer position
90+
{ word, range } = words.getWordAndRange(editor) unless word? and range?
91+
range = words.getWordRangeWithoutTrailingDots(word, range, bufferPosition)
92+
word = editor.getTextInBufferRange(range)
93+
9394
return unless words.isValidWordToInspect(word)
9495
searchDoc({word: word, mod: mod}).then (result) =>
9596
if result.error then return

lib/runtime/goto.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import { client } from '../connection'
88
import modules from './modules'
99
import { isValidScopeToInspect } from '../misc/scopes'
1010
import {
11-
wordRegex,
12-
wordRegexWithoutDotAccessor,
1311
getWordAndRange,
1412
getWordRangeAtBufferPosition,
13+
getWordRangeWithoutTrailingDots,
1514
isValidWordToInspect
1615
} from '../misc/words'
1716
import { getLocalContext } from '../misc/blocks'
@@ -82,11 +81,11 @@ class Goto {
8281
if (!this.isClientAndInkReady()) return
8382

8483
// get word without trailing dot accessors at the buffer position
85-
const { word } = getWordAndRange(editor, {
86-
bufferPosition,
87-
beginWordRegex: wordRegex,
88-
endWordRegex: wordRegexWithoutDotAccessor,
84+
let { word, range } = getWordAndRange(editor, {
85+
bufferPosition
8986
})
87+
range = getWordRangeWithoutTrailingDots(word, range, bufferPosition)
88+
word = editor.getTextInBufferRange(range)
9089

9190
// check the validity of code to be inspected
9291
if (!(isValidWordToInspect(word))) return
@@ -144,11 +143,11 @@ class Goto {
144143
if (!isValidScopeToInspect(textEditor, bufferPosition)) return
145144

146145
// get word without trailing dot accessors at the buffer position
147-
const { word, range } = getWordAndRange(textEditor, {
148-
bufferPosition,
149-
beginWordRegex: wordRegex,
150-
endWordRegex: wordRegexWithoutDotAccessor,
146+
let { word, range } = getWordAndRange(textEditor, {
147+
bufferPosition
151148
})
149+
range = getWordRangeWithoutTrailingDots(word, range, bufferPosition)
150+
word = textEditor.getTextInBufferRange(range)
152151

153152
// check the validity of code to be inspected
154153
if (!(isValidWordToInspect(word))) return

0 commit comments

Comments
 (0)