Skip to content

Commit

Permalink
doc, query editor hover
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastián Gurín committed Jun 1, 2019
1 parent ed0d900 commit 6378e5e
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 43 deletions.
2 changes: 1 addition & 1 deletion cannabis/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- [ ] issue trailingComments not working
- [ ] issue: debug() I think it won't work debugging more than once. check!

- [ ] there are two functions, includes() and contains() - decide with one and drop the other
## Road map

- [ ] define functions separately and document their signatures so we have documentation.
Expand Down
184 changes: 184 additions & 0 deletions cannabis/src/adapter/functionTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import { ASTNode } from '../astNode';
import { objectKeys } from 'misc-utils-of-mine-generic';
import { enumKeys } from 'misc-utils-of-mine-typescript';


interface Functions extends BuiltInFunctions {
isFunctionLike(arg?: ASTNode): boolean
getExtended(arg?: ASTNode): ASTNode[]
getExtendedNames(arg?: ASTNode): string[]
text(arg?: ASTNode): string
extendsAllNamed(name: string, arg?: ASTNode): boolean
extendsAnyNamed(name: string, arg?: ASTNode): boolean
getImplementations(arg?: ASTNode): ASTNode[]
getImplementationNames(arg?: ASTNode): string[]
implementsAnyNamed(name: string, arg?: ASTNode): boolean
implementsAllNamed(name: string, arg?: ASTNode): boolean
findReferences(arg?: ASTNode): ASTNode[]
sourceFile(arg?: ASTNode): ASTNode
kindName(arg?: ASTNode): string
debug(...args: any[]): true
parent(arg?: ASTNode): ASTNode
children(arg?: ASTNode): ASTNode[]
join(arr: string[], joinChar?: string): string
includes(arr: any[], item: any): boolean
compareText(actual: string, expected: string, options?: string): boolean
contains(a: string | any[], b: any): boolean
}

interface BuiltInFunctions {
/**
* Return type of current node. Example: `type() == "foo"`
*/
type(): string

/**
* Return the `sep`-separated concatenation of all attribute names of current node. The `sep` string is alway also prepended and appended for easier comparison of the result string. Example: `attr(",") == ",foo,bar,"`
*/
attrs(sep: string): string

/**
* Return depth in AST of current node (counting from 1 for the root node). Example: `depth() <= 3`
*/
depth(): number

/**
* Return position of current node among sibling (counting from 1 for the first sibling). Example: `pos() == 2`
*/
pos(): number

/**
* Check whether position of current node among sibling is `pos` (counting from 1 for the first sibling). Negative values for `pos` count from the last sibling backward, i.e., `-1` is the last sibling. Example: `nth(3)`
*/
nth(pos: number): boolean

/**
* Shorthand for `nth(1)`.
*/
first(): boolean

/**
* Shorthand for `nth(-1)`.
*/
last(): boolean

/**
* Return the number of elements in `array`. The `array` usually is either an externally passed-in parameter or a sub-query. Example: `count({nodes}) <= count(// *)`
*/
count(array: Object[]): number

/**
* Checks whether current node is somewhere below `node`, i.e., whether current node is a child or descendant of `node`. Usually, this makes sense only if `node` is an externally passed-in parameter. Example: `below({node})`.
*/
below(node: ASTNode): boolean

/**
* Checks whether current node is following `node`, i.e., whether current node comes after `node` in a standard depth-first tree visit (where parents are visited before childs). Usually, this makes sense only if `node` is an externally passed-in parameter. Example: `follows({node})`.
*/
follows(node: ASTNode): boolean

/**
* Checks whether current node is in `nodes`. Usually, `nodes` is either an externally passed-in parameter or a sub-query. Example: `in({nodes})`.
*/
in(nodes: ASTNode[]): number

/**
* Returns the sub-string of `str`, starting at `pos` with length `len`. Negative values for `pos` count from the end of the string, i.e., `-1` is the last character. Example: `substr(@foo, 0, 1) == "A"`
*/
substr(str: string, pos: number, len: number): string

/**
* Returns the index position of sub-string `sub` in string `str`, starting at `pos`. Example: `indexof(@foo, "bar", 0) >= 0`
*/
index(str: string, sub: string, pos: number): number

/**
* Returns the string `str` with whitespaces removed from begin and end. Example: `trim(@foo) == "bar"`
*/
trim(str: string): string

/**
* Returns the lower-case variant of `str`. Example: `lc(@foo) == "bar"`
*/
lc(str: string): string

/**
* Returns the upper-case variant of `str`. Example: `uc(@foo) == "BAR"`
*/
uc(str: string): string

}

var o: { [a in keyof Functions]: 1 } = {
below: 1,
follows: 1,
in: 1,
debug: 1, join: 1,
includes: 1, compareText: 1,
contains: 1,
attrs: 1, depth: 1,
pos: 1, nth: 1,
first: 1, last: 1,
count: 1, substr: 1,
index: 1, trim: 1,
lc: 1, uc: 1,
type: 1,
isFunctionLike: 1,
getExtended: 1,
getExtendedNames: 1,
text: 1,
extendsAllNamed: 1,
extendsAnyNamed: 1,
getImplementations: 1,
getImplementationNames: 1,
implementsAnyNamed: 1,
implementsAllNamed: 1,
findReferences: 1,
sourceFile: 1,
kindName: 1,
parent: 1,
children: 1,

}

export enum FunctionNames {
below = 'below',
follows = 'follows',
in = 'in',
debug = 'debug',
join = 'join',
includes = 'includes',
compareText = 'compareText',
contains = 'contains',
attrs = 'attrs',
depth = 'depth',
pos = 'pos',
nth = 'nth',
first = 'first',
last = 'last',
count = 'count',
substr = 'substr',
index = 'index',
trim = 'trim',
lc = 'lc',
uc = 'uc',
type = 'type',
isFunctionLike = 'isFunctionLike',
getExtended = 'getExtended',
getExtendedNames = 'getExtendedNames',
text = 'text',
extendsAllNamed = 'extendsAllNamed',
extendsAnyNamed = 'extendsAnyNamed',
getImplementations = 'getImplementations',
getImplementationNames = 'getImplementationNames',
implementsAnyNamed = 'implementsAnyNamed',
implementsAllNamed = 'implementsAllNamed',
findReferences = 'findReferences',
sourceFile = 'sourceFile',
kindName = 'kindName',
parent = 'parent',
children = 'children',
}

export const functionNames = enumKeys(FunctionNames)

6 changes: 2 additions & 4 deletions cannabis/src/adapter/functions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import ASTQ from 'astq'
import { compareTexts, isArray, isString, stringToObject } from 'misc-utils-of-mine-generic'
import { getExtendsRecursively, getExtendsRecursivelyNames, getImplementsAll, getImplementsAllNames, isNode, ts, tsMorph } from 'ts-simple-ast-extra'
import { getASTNodeDescendants, getASTNodeParent, getASTNodeText } from '../astNode'
import { getASTNodeDescendants, getASTNodeParent, getASTNodeText, ASTNode } from '../astNode'
import { ExecutionContext } from '../queryAst'
const stringify = require('string.ify')

export function installFunctions(astq: ASTQ, context: ExecutionContext) {

astq.func('isFunctionLike', (adapter, node, arg) => {
astq.func('isFunctionLike', (adapter, node, arg?) => {
return isNode((node || arg)) && ts.isFunctionLike((node || arg).compilerNode)
})

Expand Down Expand Up @@ -134,5 +134,3 @@ export function installFunctions(astq: ASTQ, context: ExecutionContext) {
// })
}



0 comments on commit 6378e5e

Please sign in to comment.