Change TreeSitterLanguageMode::bufferRangeForScopeAtPosition(position) to

bufferRangeForScopeAtPosition(selector, position), to match the TextMateLanguageMode
signature, extracting some selector matching logic to do so.

Needed for https://github.com/atom/toggle-quotes/issues/57
This commit is contained in:
Ashi Krishnan
2018-07-20 16:22:15 -04:00
parent 643ffd6bb6
commit 176ee2a3b3
3 changed files with 51 additions and 12 deletions

42
src/selectors.js Normal file
View File

@@ -0,0 +1,42 @@
module.exports = {selectorMatchesAnyScope, matcherForSelector}
const _ = require('underscore-plus')
/**
* Parse a selector into parts. If already parsed, returns the selector
* unmodified.
*
* @param {String|Array<String>} selector
* @returns {Array<String>} selector parts
*/
function parse (selector) {
return typeof selector === 'string'
? selector.replace(/^\./, '').split('.')
: selector
}
const always = scope => true
/**
* Return a matcher function for a selector.
*
* @param {String} selector
* @returns {(scope: String) -> Boolean} a matcher function
*/
function matcherForSelector (selector) {
const parts = parse(selector)
return selector
? scope => _.isSubset(parts, parse(scope))
: always
}
/**
* Return true iff the selector matches any provided scope.
*
* @param {String} selector
* @param {Array<String>} scopes
* @returns {Boolean} true if any scope matches the selector
*/
function selectorMatchesAnyScope (selector, scopes) {
return !selector || scopes.some(matcherForSelector(selector))
}