Skip to content

Commit 8cec8af

Browse files
committed
strip trailing dots in frontend
1 parent 530ba7e commit 8cec8af

File tree

6 files changed

+82
-50
lines changed

6 files changed

+82
-50
lines changed

lib/misc/words.js

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,85 @@ export function withWord (editor, fn) {
2121
*
2222
* `options`
2323
* - `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” (default: `wordRegex`).
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`).
2527
*/
2628
export function getWordAndRange (editor, options = {
2729
bufferPosition: undefined,
28-
wordRegex: wordRegex
30+
wordRegex: wordRegex,
31+
beginWordRegex: undefined,
32+
endWordRegex: undefined
2933
}) {
3034
// @TODO?:
3135
// The following lines are kinda iffy: The regex may or may not be well chosen
3236
// and it duplicates the efforts from atom-language-julia.
3337
// It might be better to select the current word via finding the smallest <span>
3438
// containing the bufferPosition/cursor which also has `function` or `macro` as its class.
3539
const range = options.bufferPosition ?
36-
getWordRangeAtBufferPosition(editor, options.bufferPosition, {
37-
wordRegex: options.wordRegex ? options.wordRegex : wordRegex
38-
}) :
39-
editor.getLastCursor().getCurrentWordBufferRange({
40-
wordRegex: options.wordRegex ? options.wordRegex : wordRegex
41-
})
40+
getWordRangeAtBufferPosition(editor, options.bufferPosition, options) :
41+
getWordRangeAtBufferPosition(editor, editor.getLastCursor().getBufferPosition(), options)
4242
const word = editor.getTextInBufferRange(range)
4343
return { word, range }
4444
}
4545

4646
/**
4747
* get the word under `bufferPosition` in `editor`
48-
* adapted from https://github.com/atom/atom/blob/v1.38.2/src/cursor.js#L606-L616
4948
*
5049
* `options`
51-
* - `wordRegex` {RegExp}: A RegExp indicating what constitutes a “word” (default: `wordRegex`).
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`).
5253
*/
5354
export function getWordRangeAtBufferPosition(editor, bufferPosition, options = {
54-
wordRegex: wordRegex
55+
wordRegex: wordRegex,
56+
beginWordRegex: undefined,
57+
endWordRegex: undefined,
5558
}) {
56-
const { row, column } = bufferPosition
57-
const ranges = editor.getBuffer().findAllInRangeSync(
58-
options.wordRegex ? options.wordRegex : wordRegex,
59-
new Range(new Point(row, 0), new Point(row, Infinity))
60-
)
61-
const range = ranges.find(range =>
62-
range.end.column >= column && range.start.column <= column
63-
)
64-
return range ? Range.fromObject(range) : new Range(bufferPosition, bufferPosition)
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
74+
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
89+
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)
102+
}
65103
}
66104

67105
/**

lib/runtime/datatip.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { client } from '../connection'
1212
import modules from './modules'
1313
import { isValidScopeToInspect } from '../misc/scopes'
1414
import {
15+
wordRegex,
1516
wordRegexWithoutDotAccessor,
1617
getWordAndRange,
1718
isValidWordToInspect
@@ -38,17 +39,15 @@ class DatatipProvider {
3839
// If the scope at `bufferPosition` is not valid code scope, do nothing
3940
if (!isValidScopeToInspect(editor, bufferPosition)) return
4041

41-
// get word w/ w/o dots at the buffer position
42+
// get word without trailing dot accessors at the buffer position
4243
const { range, word } = getWordAndRange(editor, {
4344
bufferPosition,
44-
wordRegex: wordRegexWithoutDotAccessor
45-
})
46-
const { word: fullWord, range: fullRange } = getWordAndRange(editor, {
47-
bufferPosition
45+
beginWordRegex: wordRegex,
46+
endWordRegex: wordRegexWithoutDotAccessor,
4847
})
4948

5049
// check the validity of code to be inspected
51-
if (!(isValidWordToInspect(word) && isValidWordToInspect(fullWord))) return
50+
if (!(isValidWordToInspect(word))) return
5251

5352
const { main, sub } = await modules.getEditorModule(editor, bufferPosition)
5453
const mod = main ? (sub ? `${main}.${sub}` : main) : 'Main'
@@ -59,7 +58,6 @@ class DatatipProvider {
5958
try {
6059
const result = await datatip({
6160
word,
62-
fullWord,
6361
mod,
6462
path: editor.getPath(),
6563
column: column + 1,
@@ -72,7 +70,7 @@ class DatatipProvider {
7270
if (result.line) {
7371
const value = editor.lineTextForBufferRow(result.line).trim()
7472
return {
75-
range: result.local ? fullRange : range,
73+
range,
7674
markedStrings: [{
7775
type: 'snippet',
7876
value,
@@ -81,7 +79,7 @@ class DatatipProvider {
8179
}
8280
} else if (result.strings) {
8381
return {
84-
range: result.local ? fullRange : range,
82+
range,
8583
markedStrings: result.strings.map(string => {
8684
return {
8785
type: string.type,
@@ -95,7 +93,7 @@ class DatatipProvider {
9593
if (result.line) {
9694
const value = editor.lineTextForBufferRow(result.line).trim()
9795
return {
98-
range: result.local ? fullRange : range,
96+
range,
9997
markedStrings: [{
10098
type: 'snippet',
10199
value,
@@ -105,7 +103,7 @@ class DatatipProvider {
105103
} else if (result.strings) {
106104
// @NOTE: atom-ide-datatip can't render multiple `snippet`s in `markedStrings` correctly
107105
return {
108-
range: result.local ? fullRange : range,
106+
range,
109107
markedStrings: result.strings.map(string => {
110108
return {
111109
type: 'markdown',

lib/runtime/evaluation.coffee

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

8787
toggleDocs: (word, range) ->
8888
{ editor, mod, edpath } = @_currentContext()
89-
{ word, range } = words.getWordAndRange(editor) unless word? and range?
90-
if word.length == 0 || !isNaN(word) then return
89+
{ word, range } = words.getWordAndRange(editor,
90+
beginWordRegex: words.wordRegex
91+
endWordRegex: words.wordRegexWithoutDotAccessor
92+
) unless word? and range?
93+
return unless words.isValidWordToInspect(word)
9194
searchDoc({word: word, mod: mod}).then (result) =>
9295
if result.error then return
9396
v = views.render result

lib/runtime/goto.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { client } from '../connection'
88
import modules from './modules'
99
import { isValidScopeToInspect } from '../misc/scopes'
1010
import {
11+
wordRegex,
1112
wordRegexWithoutDotAccessor,
1213
getWordAndRange,
1314
getWordRangeAtBufferPosition,
@@ -80,17 +81,15 @@ class Goto {
8081

8182
if (!this.isClientAndInkReady()) return
8283

83-
// get word w/ w/o dots at the buffer position
84+
// get word without trailing dot accessors at the buffer position
8485
const { word } = getWordAndRange(editor, {
8586
bufferPosition,
86-
wordRegex: wordRegexWithoutDotAccessor
87-
})
88-
const { word: fullWord } = getWordAndRange(editor, {
89-
bufferPosition
87+
beginWordRegex: wordRegex,
88+
endWordRegex: wordRegexWithoutDotAccessor,
9089
})
9190

9291
// check the validity of code to be inspected
93-
if (!(isValidWordToInspect(word) && isValidWordToInspect(fullWord))) return
92+
if (!(isValidWordToInspect(word))) return
9493

9594
// local context
9695
const { column, row } = bufferPosition
@@ -103,7 +102,6 @@ class Goto {
103102

104103
gotoSymbol({
105104
word,
106-
fullWord,
107105
path: editor.getPath(),
108106
// local context
109107
column: column + 1,
@@ -145,17 +143,15 @@ class Goto {
145143
// If the scope at `bufferPosition` is not valid code scope, do nothing
146144
if (!isValidScopeToInspect(textEditor, bufferPosition)) return
147145

148-
// get word w/ w/o dots at the buffer position
146+
// get word without trailing dot accessors at the buffer position
149147
const { word, range } = getWordAndRange(textEditor, {
150148
bufferPosition,
151-
wordRegex: wordRegexWithoutDotAccessor
152-
})
153-
const { word: fullWord, range: fullRange } = getWordAndRange(textEditor, {
154-
bufferPosition
149+
beginWordRegex: wordRegex,
150+
endWordRegex: wordRegexWithoutDotAccessor,
155151
})
156152

157153
// check the validity of code to be inspected
158-
if (!(isValidWordToInspect(word) && isValidWordToInspect(fullWord))) return
154+
if (!(isValidWordToInspect(word))) return
159155

160156
// local context
161157
const { column, row } = bufferPosition
@@ -169,7 +165,6 @@ class Goto {
169165
return new Promise((resolve) => {
170166
gotoSymbol({
171167
word,
172-
fullWord,
173168
path: textEditor.getPath(),
174169
// local context
175170
column: column + 1,
@@ -189,7 +184,7 @@ class Goto {
189184
})
190185
}
191186
resolve({
192-
range: results.local ? fullRange : range,
187+
range,
193188
callback: () => {
194189
setTimeout(() => {
195190
this.ink.goto.goto(results, {

lib/runtime/workspace.coffee

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ module.exports =
4141
mod = if @mod == modules.follow then modules.current() else (@mod or 'Main')
4242
gotoSymbol
4343
word: name,
44-
fullWord: name,
4544
mod: mod
4645
.then (symbols) =>
4746
return if symbols.error

lib/ui/docs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export function processItem (item) {
6969
item.onClickName = () => {
7070
gotoSymbol({
7171
word: item.name,
72-
fullWord: item.name,
7372
mod: item.mod
7473
}).then(symbols => {
7574
if (symbols.error) return

0 commit comments

Comments
 (0)