Package org.fife.rsta.ac.js
Class JavaScriptHelper
- java.lang.Object
-
- org.fife.rsta.ac.js.JavaScriptHelper
-
public class JavaScriptHelper extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classJavaScriptHelper.InfixVisitorVisitor for infix expression to work out whether the variable should be a string number literal Only works by determining the presence of StringLiterals and NumberLiterals.static classJavaScriptHelper.ParseTextprivate static classJavaScriptHelper.ParseTextVisitor
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringINFIX
-
Constructor Summary
Constructors Constructor Description JavaScriptHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancanResolveVariable(org.mozilla.javascript.ast.AstNode target, org.mozilla.javascript.ast.AstNode initializer)Test whether the start of the variable is the same name as the variable being initialised.static java.lang.StringconvertNodeToSource(org.mozilla.javascript.ast.AstNode node)private static TypeDeclarationcreateArrayType(org.mozilla.javascript.ast.AstNode typeNode, SourceCompletionProvider provider)Create array type from AstNode and try to determine the array typestatic TypeDeclarationcreateNewTypeDeclaration(java.lang.String newName)private static TypeDeclarationfindArrayType(org.mozilla.javascript.ast.ArrayLiteral arrayLit, SourceCompletionProvider provider)Find the array type from ArrayLiteral.static org.mozilla.javascript.ast.FunctionCallfindFunctionCallFromNode(org.mozilla.javascript.ast.AstNode node)Iterate back up through parent nodes and check whether inside a function.private static TypeDeclarationfindGetElementType(org.mozilla.javascript.ast.AstNode node, SourceCompletionProvider provider)Check the Get Element and extract the Array type from the variable e.g var a = [1, 2, 3]; var b = a[1]; //b resolves to Numberstatic intfindIndexOfFirstOpeningBracket(java.lang.String text)Returns the index of the first ( working backwards if there is no matching closing bracketstatic intfindIndexOfFirstOpeningSquareBracket(java.lang.String text)static intfindLastIndexOfJavaScriptIdentifier(java.lang.String input)private static java.lang.StringfindNewExpressionString(org.mozilla.javascript.ast.AstNode node)Returns the node name from 'Token.NEW' AstNode e.g.static TypeDeclarationfindOrMakeTypeDeclaration(java.lang.String name, SourceCompletionProvider provider)static java.lang.StringgetFunctionNameLookup(org.mozilla.javascript.ast.AstNode node, SourceCompletionProvider provider)static TypeDeclarationgetTypeDeclaration(java.lang.String name, SourceCompletionProvider provider)Convenience method to lookup TypeDeclaration through the TypeDeclarationFactory.private static TypeDeclarationgetTypeFromInFixExpression(org.mozilla.javascript.ast.AstNode node, SourceCompletionProvider provider)Use a visitor to visit all the nodes to work out which type to return e.g 1 + 1 returns Number 1 + "" returns String true returns Boolean etc.static booleanisInfixOnly(org.mozilla.javascript.ast.AstNode typeNode)static JavaScriptHelper.ParseTextparseEnteredText(java.lang.String text)Parse Text with JavaScript Parser and return AstNode from the expression etc.private static TypeDeclarationprocessNewNode(org.mozilla.javascript.ast.AstNode typeNode, SourceCompletionProvider provider)static java.lang.StringremoveLastDotFromText(java.lang.String text)static TypeDeclarationtokenToNativeTypeDeclaration(org.mozilla.javascript.ast.AstNode typeNode, SourceCompletionProvider provider)Convert AstNode to TypeDeclarationstatic java.lang.StringtrimFromLastParam(java.lang.String text)Trims the text from the last , from the string Looks for(or[starting at the end of the string to find out where in the string to substring.
-
-
-
Method Detail
-
canResolveVariable
public static boolean canResolveVariable(org.mozilla.javascript.ast.AstNode target, org.mozilla.javascript.ast.AstNode initializer)Test whether the start of the variable is the same name as the variable being initialised. This is not possible.- Parameters:
target- Name of variable being createdinitializer- name of initializer- Returns:
- true if name is different
-
parseEnteredText
public static final JavaScriptHelper.ParseText parseEnteredText(java.lang.String text)
Parse Text with JavaScript Parser and return AstNode from the expression etc.- Parameters:
text- to parse- Returns:
- expression statement text from source
-
getFunctionNameLookup
public static java.lang.String getFunctionNameLookup(org.mozilla.javascript.ast.AstNode node, SourceCompletionProvider provider)- Parameters:
node- AstNode to look for function- Returns:
- function lookup name from it's AstNode. i.e. concat function name and parameters. If no function is found, then return null
-
findFunctionCallFromNode
public static org.mozilla.javascript.ast.FunctionCall findFunctionCallFromNode(org.mozilla.javascript.ast.AstNode node)
Iterate back up through parent nodes and check whether inside a function. If the node is a function, then the Parsed parent node structure is:FunctionCall --> PropertyGet --> NameAnything other structure should be rejected.- Parameters:
node-- Returns:
-
tokenToNativeTypeDeclaration
public static final TypeDeclaration tokenToNativeTypeDeclaration(org.mozilla.javascript.ast.AstNode typeNode, SourceCompletionProvider provider)
Convert AstNode to TypeDeclaration- Parameters:
typeNode- AstNode to convertprovider- SourceProvider- Returns:
- TypeDeclaration if node resolves to supported type, e.g. Number, New etc., otherwise null
-
findGetElementType
private static TypeDeclaration findGetElementType(org.mozilla.javascript.ast.AstNode node, SourceCompletionProvider provider)
Check the Get Element and extract the Array type from the variable e.g var a = [1, 2, 3]; var b = a[1]; //b resolves to Number- Parameters:
node-provider-- Returns:
-
createArrayType
private static TypeDeclaration createArrayType(org.mozilla.javascript.ast.AstNode typeNode, SourceCompletionProvider provider)
Create array type from AstNode and try to determine the array type- Parameters:
typeNode-provider-- Returns:
-
findArrayType
private static TypeDeclaration findArrayType(org.mozilla.javascript.ast.ArrayLiteral arrayLit, SourceCompletionProvider provider)
Find the array type from ArrayLiteral. Iterates through elements and checks all the types are the same- Parameters:
arrayLit-provider-- Returns:
- TypeDeclaration if all elements are of the same type else TypeDeclarationFactory.getDefaultTypeDeclaration();
-
processNewNode
private static TypeDeclaration processNewNode(org.mozilla.javascript.ast.AstNode typeNode, SourceCompletionProvider provider)
-
findOrMakeTypeDeclaration
public static TypeDeclaration findOrMakeTypeDeclaration(java.lang.String name, SourceCompletionProvider provider)
-
createNewTypeDeclaration
public static TypeDeclaration createNewTypeDeclaration(java.lang.String newName)
-
isInfixOnly
public static boolean isInfixOnly(org.mozilla.javascript.ast.AstNode typeNode)
-
getTypeFromInFixExpression
private static TypeDeclaration getTypeFromInFixExpression(org.mozilla.javascript.ast.AstNode node, SourceCompletionProvider provider)
Use a visitor to visit all the nodes to work out which type to return e.g 1 + 1 returns Number 1 + "" returns String true returns Boolean etc.- Parameters:
node-- Returns:
-
convertNodeToSource
public static java.lang.String convertNodeToSource(org.mozilla.javascript.ast.AstNode node)
-
findIndexOfFirstOpeningBracket
public static int findIndexOfFirstOpeningBracket(java.lang.String text)
Returns the index of the first ( working backwards if there is no matching closing bracket- Parameters:
text-
-
findIndexOfFirstOpeningSquareBracket
public static int findIndexOfFirstOpeningSquareBracket(java.lang.String text)
-
findNewExpressionString
private static java.lang.String findNewExpressionString(org.mozilla.javascript.ast.AstNode node)
Returns the node name from 'Token.NEW' AstNode e.g. new Object --> Object new Date --> Date etc.- Parameters:
node- NewExpression node- Returns:
- Extracts the Name identifier from NewExpression
-
getTypeDeclaration
public static TypeDeclaration getTypeDeclaration(java.lang.String name, SourceCompletionProvider provider)
Convenience method to lookup TypeDeclaration through the TypeDeclarationFactory.- Parameters:
name-- Returns:
-
findLastIndexOfJavaScriptIdentifier
public static int findLastIndexOfJavaScriptIdentifier(java.lang.String input)
-
removeLastDotFromText
public static java.lang.String removeLastDotFromText(java.lang.String text)
- Parameters:
text- to trim- Returns:
- text up to the last dot e.g. a.getText().length returns a.getText()
-
trimFromLastParam
public static java.lang.String trimFromLastParam(java.lang.String text)
Trims the text from the last , from the string Looks for(or[starting at the end of the string to find out where in the string to substring. Do not need to trim off if inside either () or []. e.g 1, "".charAt(position).indexOf(2, "") String should be trimmed at the 1, not the 2,- Parameters:
text-- Returns:
-
-