-
Notifications
You must be signed in to change notification settings - Fork 1
Query APIs
Kim Se-Won edited this page Dec 16, 2015
·
8 revisions
Once we have analyzed a program and get the result from getAnalysisResult, we can obtain useful information from result by using several APIs.
Here are several API functions that calcium provides.
-
findVarRefsAt(result.AST, pos): Given an annotated AST and a character position,findVarRefsAtfinds the occurrences of the variable at the position. - If the position is not at a variable, it returns
null. - Otherwise, it returns an array of regions of the occurrences. For example, the following represents two occurrence at position 30 and 38.
[ {start: 30, end: 31}, {start: 38, end: 39} ]- The array has an additional property
varNamewhose value is the variable name at the position. - This API is used for renaming variables, highlighting same variables, and selecting variable occurrences in Webida's calcium plugin.
-
findEscapingStatements(result.AST, pos): Given an annotated AST and a character position,findEscapingStatementsfinds the statements that escape the function or the statements caught by some catch block. - If the position is not relevant (not on keywords like
return,throwand etc.), it returnsnull. - Otherwise, it returns an array of regions like
findVarRefsAtdoes. -
findThisExpressions(result.AST, pos, includeFunctionKeyword):findThisExpressionsfinds the same occurrences of thethisatposfrom the AST. The return value is an array of regions similar tofindEscapingStatements. The last parameterincludeFunctionKeywordis a boolean value whether the array should additionally contain the region offunctionkeyword that owns our interestedthis. -
getTypeData(result.AST, result.Ĉ, startPos, endPos):getTypeDatacomputes the type information at the range fromstartPostoendPos. It returns an object like
{ hasType: true, nodeStart: 57, nodeEnd: 63, typeString: 'number' }-
nodeStartandnodeEndare the start and end position of the AST node that can cover the range fromstartPostoendPos. -
hasTypeistruewhen the found AST node is a valid node to have types. For example, if the node is try block,hasType will befalse`. -
typeStringis the string representation for the types of the found node. WhenhasTypeis false,typeStringcontains the message"No types at the given range". -
getFnTypeStructuresAt(result.AST, result.Ĉ, pos):getFnTypeStructuresAtis similar togetTypeDataexcept two things. - It returns an array of function types that are possible at
pos. - Each function type is an object with properties
paramsandret.getFnTypeStructuresAtis used by argument hints of calcium plugin. For example, assume we are editing the following program.
function add(x,y) { return x + y; }
add(1,1);
add(_ // here, underscore denotes the cursorThen, the return value of getFnTypeStructuresAt is
[
{ params: [ { name: "x", type: "number" },
{ name: "y", type: "number" } ],
ret: "number" }
]Using this object, we can create the argument hint. Actually, we can use getTypeData and parse the type string. But such approach requires a parser for type strings and is error-prone.
-
getDefinitionSitesAt(result.AST, result.Ĉ, startPos, endPos):getDefinitionSitesAtfirst finds the types at the range fromstartPostoendPos. Then, it returns an array of the definition sites of the function types. Each definition site is an object with propertiesstart,endandat.startandenddenotes the start and end of the definition andatis the position that we will jump to. For function declarationatwill be the start of declared function name. For function expressionatwill be the start of thefunctionkeyword. -
getCompletionAtPos(result, pos):getCompletionreturns the list of completion candidates atpos. Let us show an example return value.
{
from: 100,
to: 100,
list: [
{ text: "x", icon: "number" },
{ text: "y", icon: "bool" },
{ text: "id", icon: "fn" },
{ text: "add", icon: "fn" }
]
}-
fromandtois the text range that will be replaced by the selected completion item. In the previous case, we will simply insert the text sincefromandtoare equal. -
listcontains the completion candidates. When we select an item, we replace the range with the string value oftext. The string value oficonis a simple type hint of the candidate.