Skip to content

Commit

Permalink
🆕 Determine word regexp based on position
Browse files Browse the repository at this point in the history
  • Loading branch information
steelbrain committed Jun 23, 2016
1 parent 56f95b1 commit 3f6510f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
"never"
],
"prefer-arrow-callback": 0,
"max-len": [2, 200]
"max-len": [
2,
200
]
},
"extends": "airbnb/base",
"parser": "babel-eslint",
Expand Down
15 changes: 14 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import FS from 'fs'
import Temp from 'tmp'
import promisify from 'sb-promisify'
import type { TextEditor } from 'atom'
import type { TextEditor, Range } from 'atom'
import type { TempDirectory } from './types'

export const writeFile = promisify(FS.writeFile)
Expand All @@ -19,6 +19,19 @@ export const assign = Object.assign || function (target, source) {
return target
}

function escapeRegexp(string: string): string {
// Shamelessly stolen from https://github.com/atom/underscore-plus/blob/130913c179fe1d718a14034f4818adaf8da4db12/src/underscore-plus.coffee#L138
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
}

export function getWordRegexp(textEditor: TextEditor, bufferPosition: Range) {
const scopeDescriptor = textEditor.scopeDescriptorForBufferPosition(bufferPosition)
const nonWordCharacters = escapeRegexp(atom.config.get('editor.nonWordCharacters', {
scope: scopeDescriptor
}))
return new RegExp(`^[\t ]*$|[^\\s${nonWordCharacters}]+`)
}

export function getTempDirectory(prefix: string): Promise<TempDirectory> {
return new Promise(function (resolve, reject) {
Temp.dir({ prefix }, function (error, directory, cleanup) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function rangeFromLineNumber(textEditor: TextEditor, line: number, column

let colEnd = lineLength
const rowText = buffer.lineForRow(lineNumber).substr(colStart)
const match = /^( +|[\$\w]+)/.exec(rowText)
const match = Helpers.getWordRegexp(textEditor, [lineNumber, colStart]).exec(rowText)
if (match) {
colEnd = colStart + match.index + match[0].length
}
Expand Down

0 comments on commit 3f6510f

Please sign in to comment.