Skip to content

Commit 5529aff

Browse files
committed
strip trailing dots
1 parent 0ceb274 commit 5529aff

File tree

4 files changed

+84
-30
lines changed

4 files changed

+84
-30
lines changed

lib/misc/words.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import { Point, Range } from 'atom'
44

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

78
/**
89
* Takes an `editor` and gets the word at current cursor position. If that is nonempty, call
910
* function `fn` with arguments `word` and `range`.
1011
*/
1112
export function withWord (editor, fn) {
12-
const { word, range } = getWord(editor)
13+
const { word, range } = getWordAndRange(editor)
1314
// If we only find numbers or nothing, return prematurely
1415
if (!isValidWordToInspect(word)) return
1516
fn(word, range)
@@ -18,17 +19,26 @@ export function withWord (editor, fn) {
1819
/**
1920
* Gets the word and its range in the `editor`
2021
*
21-
* `bufferPosition` {Point}: If given returns the word at the `bufferPosition`, returns the word at the current cursor otherwise.
22+
* `options`
23+
* - `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`).
2225
*/
23-
export function getWord (editor, bufferPosition) {
26+
export function getWordAndRange (editor, options = {
27+
bufferPosition: undefined,
28+
wordRegex: wordRegex
29+
}) {
2430
// @TODO?:
2531
// The following lines are kinda iffy: The regex may or may not be well chosen
2632
// and it duplicates the efforts from atom-language-julia.
2733
// It might be better to select the current word via finding the smallest <span>
2834
// 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 })
35+
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+
})
3242
const word = editor.getTextInBufferRange(range)
3343
return { word, range }
3444
}
@@ -37,12 +47,15 @@ export function getWord (editor, bufferPosition) {
3747
* get the word under `bufferPosition` in `editor`
3848
* adapted from https://github.com/atom/atom/blob/v1.38.2/src/cursor.js#L606-L616
3949
*
40-
* - optionalWordRegex: if not given, the toplevel `wordRegex` would be used
50+
* `options`
51+
* - `wordRegex` {RegExp}: A RegExp indicating what constitutes a “word” (default: `wordRegex`).
4152
*/
42-
export function getWordRangeAtBufferPosition(editor, bufferPosition, optionalWordRegex) {
53+
export function getWordRangeAtBufferPosition(editor, bufferPosition, options = {
54+
wordRegex: wordRegex
55+
}) {
4356
const { row, column } = bufferPosition
4457
const ranges = editor.getBuffer().findAllInRangeSync(
45-
optionalWordRegex || wordRegex,
58+
options.wordRegex ? options.wordRegex : wordRegex,
4659
new Range(new Point(row, 0), new Point(row, Infinity))
4760
)
4861
const range = ranges.find(range =>

lib/runtime/datatip.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import { client } from '../connection'
1212
import modules from './modules'
1313
import { isValidScopeToInspect } from '../misc/scopes'
14-
import { getWord, isValidWordToInspect } from '../misc/words'
14+
import {
15+
wordRegexWithoutDotAccessor,
16+
getWordAndRange,
17+
isValidWordToInspect
18+
} from '../misc/words'
1519
import { getLocalContext } from '../misc/blocks'
1620

1721
const datatip = client.import('datatip')
@@ -34,9 +38,17 @@ class DatatipProvider {
3438
// If the scope at `bufferPosition` is not valid code scope, do nothing
3539
if (!isValidScopeToInspect(editor, bufferPosition)) return
3640

37-
// Check the validity of code to be inspected
38-
const { range, word } = getWord(editor, bufferPosition)
39-
if (!isValidWordToInspect(word)) return
41+
// get word w/ w/o dots at the buffer position
42+
const { range, word } = getWordAndRange(editor, {
43+
bufferPosition,
44+
wordRegex: wordRegexWithoutDotAccessor
45+
})
46+
const { word: fullWord, range: fullRange } = getWordAndRange(editor, {
47+
bufferPosition
48+
})
49+
50+
// check the validity of code to be inspected
51+
if (!(isValidWordToInspect(word) && isValidWordToInspect(fullWord))) return
4052

4153
const { main, sub } = await modules.getEditorModule(editor, bufferPosition)
4254
const mod = main ? (sub ? `${main}.${sub}` : main) : 'Main'
@@ -47,6 +59,7 @@ class DatatipProvider {
4759
try {
4860
const result = await datatip({
4961
word,
62+
fullWord,
5063
mod,
5164
path: editor.getPath(),
5265
column: column + 1,
@@ -59,7 +72,7 @@ class DatatipProvider {
5972
if (result.line) {
6073
const value = editor.lineTextForBufferRow(result.line).trim()
6174
return {
62-
range,
75+
range: result.local ? fullRange : range,
6376
markedStrings: [{
6477
type: 'snippet',
6578
value,
@@ -68,7 +81,7 @@ class DatatipProvider {
6881
}
6982
} else if (result.strings) {
7083
return {
71-
range,
84+
range: result.local ? fullRange : range,
7285
markedStrings: result.strings.map(string => {
7386
return {
7487
type: string.type,
@@ -82,7 +95,7 @@ class DatatipProvider {
8295
if (result.line) {
8396
const value = editor.lineTextForBufferRow(result.line).trim()
8497
return {
85-
range,
98+
range: result.local ? fullRange : range,
8699
markedStrings: [{
87100
type: 'snippet',
88101
value,
@@ -92,7 +105,7 @@ class DatatipProvider {
92105
} else if (result.strings) {
93106
// @NOTE: atom-ide-datatip can't render multiple `snippet`s in `markedStrings` correctly
94107
return {
95-
range,
108+
range: result.local ? fullRange : range,
96109
markedStrings: result.strings.map(string => {
97110
return {
98111
type: 'markdown',

lib/runtime/evaluation.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ module.exports =
8585
workspace.update()
8686

8787
toggleDocs: (word, range) ->
88-
{editor, mod, edpath} = @_currentContext()
89-
{word, range} = words.getWord(editor) unless word? and range?
88+
{ editor, mod, edpath } = @_currentContext()
89+
{ word, range } = words.getWordAndRange(editor) unless word? and range?
9090
if word.length == 0 || !isNaN(word) then return
9191
searchDoc({word: word, mod: mod}).then (result) =>
9292
if result.error then return

lib/runtime/goto.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { CompositeDisposable, Range } from 'atom'
77
import { client } from '../connection'
88
import modules from './modules'
99
import { isValidScopeToInspect } from '../misc/scopes'
10-
import { getWord, getWordRangeAtBufferPosition, isValidWordToInspect } from '../misc/words'
10+
import {
11+
wordRegexWithoutDotAccessor,
12+
getWordAndRange,
13+
getWordRangeAtBufferPosition,
14+
isValidWordToInspect
15+
} from '../misc/words'
1116
import { getLocalContext } from '../misc/blocks'
1217

1318
const {
@@ -34,11 +39,15 @@ class Goto {
3439
}
3540

3641
getJumpFilePath(editor, bufferPosition) {
37-
const includeRange = getWordRangeAtBufferPosition(editor, bufferPosition, includeRegex)
42+
const includeRange = getWordRangeAtBufferPosition(editor, bufferPosition, {
43+
wordRegex: includeRegex
44+
})
3845
if (includeRange.isEmpty()) return false
3946

4047
// return if the bufferPosition is not on the path string
41-
const filePathRange = getWordRangeAtBufferPosition(editor, bufferPosition, filePathRegex)
48+
const filePathRange = getWordRangeAtBufferPosition(editor, bufferPosition, {
49+
wordRegex: filePathRegex
50+
})
4251
if (filePathRange.isEmpty()) return false
4352

4453
const filePathText = editor.getTextInBufferRange(filePathRange)
@@ -71,8 +80,17 @@ class Goto {
7180

7281
if (!this.isClientAndInkReady()) return
7382

74-
const { word } = getWord(editor, bufferPosition)
75-
if (!isValidWordToInspect(word)) return
83+
// get word w/ w/o dots at the buffer position
84+
const { word } = getWordAndRange(editor, {
85+
bufferPosition,
86+
wordRegex: wordRegexWithoutDotAccessor
87+
})
88+
const { word: fullWord } = getWordAndRange(editor, {
89+
bufferPosition
90+
})
91+
92+
// check the validity of code to be inspected
93+
if (!(isValidWordToInspect(word) && isValidWordToInspect(fullWord))) return
7694

7795
// local context
7896
const { column, row } = bufferPosition
@@ -85,6 +103,7 @@ class Goto {
85103

86104
gotoSymbol({
87105
word,
106+
fullWord,
88107
path: editor.getPath(),
89108
// local context
90109
column: column + 1,
@@ -123,12 +142,20 @@ class Goto {
123142
// If Julia is not running, do nothing
124143
if (!this.isClientAndInkReady()) return
125144

126-
const { word, range } = getWord(textEditor, bufferPosition)
127-
128145
// If the scope at `bufferPosition` is not valid code scope, do nothing
129146
if (!isValidScopeToInspect(textEditor, bufferPosition)) return
130-
// Check the validity of code to be inspected
131-
if (!isValidWordToInspect(word)) return
147+
148+
// get word w/ w/o dots at the buffer position
149+
const { word, range } = getWordAndRange(textEditor, {
150+
bufferPosition,
151+
wordRegex: wordRegexWithoutDotAccessor
152+
})
153+
const { word: fullWord, range: fullRange } = getWordAndRange(textEditor, {
154+
bufferPosition
155+
})
156+
157+
// check the validity of code to be inspected
158+
if (!(isValidWordToInspect(word) && isValidWordToInspect(fullWord))) return
132159

133160
// local context
134161
const { column, row } = bufferPosition
@@ -139,9 +166,10 @@ class Goto {
139166
const mod = main ? (sub ? `${main}.${sub}` : main) : 'Main'
140167
const text = textEditor.getText() // buffer text that will be used for fallback entry
141168

142-
return new Promise((resolve, reject) => {
169+
return new Promise((resolve) => {
143170
gotoSymbol({
144171
word,
172+
fullWord,
145173
path: textEditor.getPath(),
146174
// local context
147175
column: column + 1,
@@ -161,7 +189,7 @@ class Goto {
161189
})
162190
}
163191
resolve({
164-
range,
192+
range: results.local ? fullRange : range,
165193
callback: () => {
166194
setTimeout(() => {
167195
this.ink.goto.goto(results, {

0 commit comments

Comments
 (0)