Skip to content

Commit

Permalink
npm run build
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Apr 13, 2020
1 parent b98e124 commit e82fa0f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
11 changes: 11 additions & 0 deletions lib/misc/scopes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @babel */
import { TextEditor, PointCompatible } from "atom";
export declare function isStringScope(scopes: readonly string[]): boolean;
export declare function forLines(editor: TextEditor, start: number, end: number): string[];
export declare function isCommentScope(scopes: readonly string[]): boolean;
/**
* Returns `true` if the scope at `bufferPosition` in `editor` is valid code scope to be inspected.
* Supposed to be used within Atom-IDE integrations, whose `grammarScopes` setting doesn't support
* embedded scopes by default.
*/
export declare function isValidScopeToInspect(editor: TextEditor, bufferPosition: PointCompatible): boolean;
53 changes: 29 additions & 24 deletions lib/misc/scopes.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
/** @babel */

import { Point, Range } from 'atom'

const juliaScopes = ['source.julia', 'source.embedded.julia']
const openers = [
'if', 'while', 'for', 'begin', 'function', 'macro', 'module', 'baremodule', 'type', 'immutable',
'struct', 'mutable struct', 'try', 'let', 'do', 'quote', 'abstract type', 'primitive type'
'if',
'while',
'for',
'begin',
'function',
'macro',
'module',
'baremodule',
'type',
'immutable',
'struct',
'mutable struct',
'try',
'let',
'do',
'quote',
'abstract type',
'primitive type'
]
const reopeners = [ 'else', 'elseif', 'catch', 'finally' ]

function isKeywordScope (scopes) {
const reopeners = ['else', 'elseif', 'catch', 'finally']
function isKeywordScope(scopes) {
// Skip 'source.julia'
return scopes.slice(1).some(scope => {
return scope.indexOf('keyword') > -1
})
}

export function isStringScope (scopes) {
export function isStringScope(scopes) {
let isString = false
let isInterp = false
for (const scope of scopes) {
Expand All @@ -29,14 +41,11 @@ export function isStringScope (scopes) {
}
return isString && !isInterp
}

function forRange (editor, range) {
function forRange(editor, range) {
// this should happen here and not a top-level so that we aren't relying on
// Atom to load packages in a specific order:
const juliaGrammar = atom.grammars.grammarForScopeName('source.julia')

if (juliaGrammar === undefined) return []

const scopes = []
let n_parens = 0
let n_brackets = 0
Expand All @@ -63,43 +72,39 @@ function forRange (editor, range) {
return
}
}
if (!(isKeywordScope(token.scopes))) return
if (!isKeywordScope(token.scopes)) return
if (!(n_parens === 0 && n_brackets === 0)) return

const reopen = reopeners.includes(value)
if (value === 'end' || reopen) scopes.pop()
if (openers.includes(value) || reopen) scopes.push(value)
})
})
return scopes
}

export function forLines (editor, start, end) {
export function forLines(editor, start, end) {
const startPoint = new Point(start, 0)
const endPoint = new Point(end, Infinity)
const range = new Range(startPoint, endPoint)
return forRange(editor, range)
}

export function isCommentScope (scopes) {
export function isCommentScope(scopes) {
// Skip 'source.julia'
return scopes.slice(1).some(scope => {
return scope.indexOf('comment') > -1
})
}

/**
* Returns `true` if the scope at `bufferPosition` in `editor` is valid code scope to be inspected.
* Supposed to be used within Atom-IDE integrations, whose `grammarScopes` setting doesn't support
* embedded scopes by default.
*/
export function isValidScopeToInspect (editor, bufferPosition) {
export function isValidScopeToInspect(editor, bufferPosition) {
const scopes = editor
.scopeDescriptorForBufferPosition(bufferPosition)
.getScopesArray()
return scopes.some(scope => {
return juliaScopes.includes(scope)
}) ?
!isCommentScope(scopes) && !isStringScope(scopes) :
false
})
? !isCommentScope(scopes) && !isStringScope(scopes)
: false
}

0 comments on commit e82fa0f

Please sign in to comment.