mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-18 17:27:54 -05:00
Merge branch 'master' into colectomy
This commit is contained in:
@@ -38,6 +38,7 @@ module ts {
|
||||
getFullWidth(): number;
|
||||
getLeadingTriviaWidth(sourceFile?: SourceFile): number;
|
||||
getFullText(sourceFile?: SourceFile): string;
|
||||
getText(sourceFile?: SourceFile): string;
|
||||
getFirstToken(sourceFile?: SourceFile): Node;
|
||||
getLastToken(sourceFile?: SourceFile): Node;
|
||||
}
|
||||
@@ -130,6 +131,10 @@ module ts {
|
||||
return (sourceFile || this.getSourceFile()).text.substring(this.pos, this.end);
|
||||
}
|
||||
|
||||
public getText(sourceFile?: SourceFile): string {
|
||||
return (sourceFile || this.getSourceFile()).text.substring(this.getStart(), this.getEnd());
|
||||
}
|
||||
|
||||
private addSyntheticNodes(nodes: Node[], pos: number, end: number): number {
|
||||
scanner.setTextPos(pos);
|
||||
while (pos < end) {
|
||||
@@ -1287,7 +1292,6 @@ module ts {
|
||||
position: number; // position in the file where the completion was requested
|
||||
entries: CompletionEntry[]; // entries for this completion
|
||||
symbols: Map<Symbol>; // symbols by entry name map
|
||||
location: Node; // the node where the completion was requested
|
||||
typeChecker: TypeChecker; // the typeChecker used to generate this completion
|
||||
}
|
||||
|
||||
@@ -1957,21 +1961,21 @@ module ts {
|
||||
}
|
||||
|
||||
function isRightSideOfPropertyAccess(node: Node) {
|
||||
return node.parent.kind === SyntaxKind.PropertyAccess && (<PropertyAccess>node.parent).right === node;
|
||||
return node && node.parent && node.parent.kind === SyntaxKind.PropertyAccess && (<PropertyAccess>node.parent).right === node;
|
||||
}
|
||||
|
||||
function isCallExpressionTarget(node: Node): boolean {
|
||||
if (isRightSideOfPropertyAccess(node)) {
|
||||
node = node.parent;
|
||||
}
|
||||
return node.parent.kind === SyntaxKind.CallExpression && (<CallExpression>node.parent).func === node;
|
||||
return node && node.parent && node.parent.kind === SyntaxKind.CallExpression && (<CallExpression>node.parent).func === node;
|
||||
}
|
||||
|
||||
function isNewExpressionTarget(node: Node): boolean {
|
||||
if (isRightSideOfPropertyAccess(node)) {
|
||||
node = node.parent;
|
||||
}
|
||||
return node.parent.kind === SyntaxKind.NewExpression && (<CallExpression>node.parent).func === node;
|
||||
return node && node.parent && node.parent.kind === SyntaxKind.NewExpression && (<CallExpression>node.parent).func === node;
|
||||
}
|
||||
|
||||
function isNameOfModuleDeclaration(node: Node) {
|
||||
@@ -2014,6 +2018,37 @@ module ts {
|
||||
(node.parent.kind === SyntaxKind.ImportDeclaration && (<ImportDeclaration>node.parent).externalModuleName === node));
|
||||
}
|
||||
|
||||
/** Returns true if the position is within a comment */
|
||||
function isInsideComment(sourceFile: SourceFile, token: Node, position: number): boolean {
|
||||
// The position has to be: 1. in the leading trivia (before token.getStart()), and 2. within a comment
|
||||
return position <= token.getStart(sourceFile) &&
|
||||
(isInsideCommentRange(getTrailingCommentRanges(sourceFile.text, token.getFullStart())) ||
|
||||
isInsideCommentRange(getLeadingCommentRanges(sourceFile.text, token.getFullStart())));
|
||||
|
||||
function isInsideCommentRange(comments: CommentRange[]): boolean {
|
||||
return forEach(comments, comment => {
|
||||
// either we are 1. completely inside the comment, or 2. at the end of the comment
|
||||
if (comment.pos < position && position < comment.end) {
|
||||
return true;
|
||||
}
|
||||
else if (position === comment.end) {
|
||||
var text = sourceFile.text;
|
||||
var width = comment.end - comment.pos;
|
||||
// is single line comment or just /*
|
||||
if (width <= 2 || text.charCodeAt(comment.pos + 1) === CharacterCodes.slash) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// is unterminated multi-line comment
|
||||
return !(text.charCodeAt(comment.end - 1) === CharacterCodes.slash &&
|
||||
text.charCodeAt(comment.end - 2) === CharacterCodes.asterisk);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
enum SemanticMeaning {
|
||||
None = 0x0,
|
||||
Value = 0x1,
|
||||
@@ -2317,6 +2352,135 @@ module ts {
|
||||
}
|
||||
|
||||
function getCompletionsAtPosition(filename: string, position: number, isMemberCompletion: boolean) {
|
||||
synchronizeHostData();
|
||||
|
||||
filename = TypeScript.switchToForwardSlashes(filename);
|
||||
|
||||
var sourceFile = getSourceFile(filename);
|
||||
|
||||
var currentToken = getTokenAtPosition(sourceFile, position);
|
||||
|
||||
// Completion not allowed inside comments, bail out if this is the case
|
||||
if (isInsideComment(sourceFile, currentToken, position)) {
|
||||
host.log("Returning an empty list because completion was inside a comment.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// The decision to provide completion depends on the previous token, so find it
|
||||
// Note: previousToken can be undefined if we are the beginning of the file
|
||||
var previousToken = findPrecedingToken(position, sourceFile);
|
||||
|
||||
// The caret is at the end of an identifier; this is a partial identifier that we want to complete: e.g. a.toS|
|
||||
// Skip this partial identifier to the previous token
|
||||
if (previousToken && position <= previousToken.end && previousToken.kind === SyntaxKind.Identifier) {
|
||||
previousToken = findPrecedingToken(previousToken.pos, sourceFile);
|
||||
}
|
||||
|
||||
// Check if this is a valid completion location
|
||||
if (previousToken && isCompletionListBlocker(previousToken)) {
|
||||
host.log("Returning an empty list because completion was requested in an invalid position.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Find the node where completion is requested on, in the case of a completion after a dot, it is the member access expression
|
||||
// other wise, it is a request for all visible symbols in the scope, and the node is the current location
|
||||
var node: Node;
|
||||
var isRightOfDot: boolean;
|
||||
if (previousToken && previousToken.kind === SyntaxKind.DotToken &&
|
||||
(previousToken.parent.kind === SyntaxKind.PropertyAccess || previousToken.parent.kind === SyntaxKind.QualifiedName)) {
|
||||
node = (<PropertyAccess>previousToken.parent).left;
|
||||
isRightOfDot = true;
|
||||
}
|
||||
else {
|
||||
node = currentToken;
|
||||
isRightOfDot = false;
|
||||
}
|
||||
|
||||
// Clear the current activeCompletionSession for this session
|
||||
activeCompletionSession = {
|
||||
filename: filename,
|
||||
position: position,
|
||||
entries: [],
|
||||
symbols: {},
|
||||
typeChecker: typeInfoResolver
|
||||
};
|
||||
|
||||
// Populate the completion list
|
||||
if (isRightOfDot) {
|
||||
// Right of dot member completion list
|
||||
var symbols: Symbol[] = [];
|
||||
isMemberCompletion = true;
|
||||
|
||||
if (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName || node.kind === SyntaxKind.PropertyAccess) {
|
||||
var symbol = typeInfoResolver.getSymbolInfo(node);
|
||||
|
||||
// This is an alias, follow what it aliases
|
||||
if (symbol && symbol.flags & SymbolFlags.Import) {
|
||||
symbol = typeInfoResolver.getAliasedSymbol(symbol);
|
||||
}
|
||||
|
||||
if (symbol && symbol.flags & SymbolFlags.HasExports) {
|
||||
// Extract module or enum members
|
||||
forEachValue(symbol.exports, symbol => {
|
||||
if (typeInfoResolver.isValidPropertyAccess(<PropertyAccess>(node.parent), symbol.name)) {
|
||||
symbols.push(symbol);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var type = typeInfoResolver.getTypeOfNode(node);
|
||||
if (type) {
|
||||
// Filter private properties
|
||||
forEach(type.getApparentProperties(), symbol => {
|
||||
if (typeInfoResolver.isValidPropertyAccess(<PropertyAccess>(node.parent), symbol.name)) {
|
||||
symbols.push(symbol);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getCompletionEntriesFromSymbols(symbols, activeCompletionSession);
|
||||
}
|
||||
else {
|
||||
var containingObjectLiteral = getContainingObjectLiteralApplicableForCompletion(previousToken);
|
||||
if (containingObjectLiteral) {
|
||||
// Object literal expression, look up possible property names from contextual type
|
||||
isMemberCompletion = true;
|
||||
|
||||
var contextualType = typeInfoResolver.getContextualType(containingObjectLiteral);
|
||||
if (!contextualType) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var contextualTypeMembers = typeInfoResolver.getPropertiesOfType(contextualType);
|
||||
if (contextualTypeMembers && contextualTypeMembers.length > 0) {
|
||||
// Add filtered items to the completion list
|
||||
var filteredMembers = filterContextualMembersList(contextualTypeMembers, containingObjectLiteral.properties);
|
||||
getCompletionEntriesFromSymbols(filteredMembers, activeCompletionSession);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Get scope members
|
||||
isMemberCompletion = false;
|
||||
|
||||
/// TODO filter meaning based on the current context
|
||||
var symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Import;
|
||||
var symbols = typeInfoResolver.getSymbolsInScope(node, symbolMeanings);
|
||||
|
||||
getCompletionEntriesFromSymbols(symbols, activeCompletionSession);
|
||||
}
|
||||
}
|
||||
|
||||
// Add keywords if this is not a member completion list
|
||||
if (!isMemberCompletion) {
|
||||
Array.prototype.push.apply(activeCompletionSession.entries, keywordCompletions);
|
||||
}
|
||||
|
||||
return {
|
||||
isMemberCompletion: isMemberCompletion,
|
||||
entries: activeCompletionSession.entries
|
||||
};
|
||||
|
||||
function getCompletionEntriesFromSymbols(symbols: Symbol[], session: CompletionSession): void {
|
||||
forEach(symbols, symbol => {
|
||||
var entry = createCompletionEntry(symbol, session.typeChecker);
|
||||
@@ -2327,40 +2491,47 @@ module ts {
|
||||
});
|
||||
}
|
||||
|
||||
function isCompletionListBlocker(sourceUnit: TypeScript.SourceUnitSyntax, position: number): boolean {
|
||||
// We shouldn't be getting a position that is outside the file because
|
||||
// isEntirelyInsideComment can't handle when the position is out of bounds,
|
||||
// callers should be fixed, however we should be resilient to bad inputs
|
||||
// so we return true (this position is a blocker for getting completions)
|
||||
if (position < 0 || position > TypeScript.fullWidth(sourceUnit)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// This method uses Fidelity completely. Some information can be reached using the AST, but not everything.
|
||||
return TypeScript.Syntax.isEntirelyInsideComment(sourceUnit, position) ||
|
||||
TypeScript.Syntax.isEntirelyInStringOrRegularExpressionLiteral(sourceUnit, position) ||
|
||||
isIdentifierDefinitionLocation(sourceUnit, position) ||
|
||||
isRightOfIllegalDot(sourceUnit, position);
|
||||
function isCompletionListBlocker(previousToken: Node): boolean {
|
||||
return isInStringOrRegularExpressionLiteral(previousToken) ||
|
||||
isIdentifierDefinitionLocation(previousToken) ||
|
||||
isRightOfIllegalDot(previousToken);
|
||||
}
|
||||
|
||||
function getContainingObjectLiteralApplicableForCompletion(sourceUnit: TypeScript.SourceUnitSyntax, position: number): TypeScript.ISyntaxElement {
|
||||
function isInStringOrRegularExpressionLiteral(previousToken: Node): boolean {
|
||||
if (previousToken.kind === SyntaxKind.StringLiteral) {
|
||||
// The position has to be either: 1. entirely within the token text, or
|
||||
// 2. at the end position, and the string literal is not terminated
|
||||
var start = previousToken.getStart();
|
||||
var end = previousToken.getEnd();
|
||||
if (start < position && position < end) {
|
||||
return true;
|
||||
}
|
||||
else if (position === end) {
|
||||
var width = end - start;
|
||||
var text = previousToken.getSourceFile().text;
|
||||
return width <= 1 ||
|
||||
text.charCodeAt(start) !== text.charCodeAt(end - 1) ||
|
||||
text.charCodeAt(end - 2) === CharacterCodes.backslash;
|
||||
}
|
||||
}
|
||||
else if (previousToken.kind === SyntaxKind.RegularExpressionLiteral) {
|
||||
return previousToken.getStart() < position && position < previousToken.getEnd();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getContainingObjectLiteralApplicableForCompletion(previousToken: Node): ObjectLiteral {
|
||||
// The locations in an object literal expression that are applicable for completion are property name definition locations.
|
||||
var previousToken = getNonIdentifierCompleteTokenOnLeft(sourceUnit, position);
|
||||
|
||||
if (previousToken) {
|
||||
var parent = previousToken.parent;
|
||||
|
||||
switch (previousToken.kind()) {
|
||||
case TypeScript.SyntaxKind.OpenBraceToken: // var x = { |
|
||||
case TypeScript.SyntaxKind.CommaToken: // var x = { a: 0, |
|
||||
if (parent && parent.kind() === TypeScript.SyntaxKind.SeparatedList) {
|
||||
parent = parent.parent;
|
||||
switch (previousToken.kind) {
|
||||
case SyntaxKind.OpenBraceToken: // var x = { |
|
||||
case SyntaxKind.CommaToken: // var x = { a: 0, |
|
||||
if (parent && parent.kind === SyntaxKind.ObjectLiteral) {
|
||||
return <ObjectLiteral>parent;
|
||||
}
|
||||
|
||||
if (parent && parent.kind() === TypeScript.SyntaxKind.ObjectLiteralExpression) {
|
||||
return parent;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -2368,47 +2539,71 @@ module ts {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function isIdentifierDefinitionLocation(sourceUnit: TypeScript.SourceUnitSyntax, position: number): boolean {
|
||||
var positionedToken = getNonIdentifierCompleteTokenOnLeft(sourceUnit, position);
|
||||
function isFunction(kind: SyntaxKind): boolean {
|
||||
switch (kind) {
|
||||
case SyntaxKind.FunctionExpression:
|
||||
case SyntaxKind.ArrowFunction:
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
case SyntaxKind.Method:
|
||||
case SyntaxKind.Constructor:
|
||||
case SyntaxKind.GetAccessor:
|
||||
case SyntaxKind.SetAccessor:
|
||||
case SyntaxKind.CallSignature:
|
||||
case SyntaxKind.ConstructSignature:
|
||||
case SyntaxKind.IndexSignature:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (positionedToken) {
|
||||
var containingNodeKind = TypeScript.Syntax.containingNode(positionedToken) && TypeScript.Syntax.containingNode(positionedToken).kind();
|
||||
switch (positionedToken.kind()) {
|
||||
case TypeScript.SyntaxKind.CommaToken:
|
||||
return containingNodeKind === TypeScript.SyntaxKind.ParameterList ||
|
||||
containingNodeKind === TypeScript.SyntaxKind.VariableDeclaration ||
|
||||
containingNodeKind === TypeScript.SyntaxKind.EnumDeclaration; // enum { foo, |
|
||||
function isIdentifierDefinitionLocation(previousToken: Node): boolean {
|
||||
if (previousToken) {
|
||||
var containingNodeKind = previousToken.parent.kind;
|
||||
switch (previousToken.kind) {
|
||||
case SyntaxKind.CommaToken:
|
||||
return containingNodeKind === SyntaxKind.VariableDeclaration ||
|
||||
containingNodeKind === SyntaxKind.VariableStatement ||
|
||||
containingNodeKind === SyntaxKind.EnumDeclaration || // enum a { foo, |
|
||||
isFunction(containingNodeKind);
|
||||
|
||||
case TypeScript.SyntaxKind.OpenParenToken:
|
||||
return containingNodeKind === TypeScript.SyntaxKind.ParameterList ||
|
||||
containingNodeKind === TypeScript.SyntaxKind.CatchClause;
|
||||
case SyntaxKind.OpenParenToken:
|
||||
return containingNodeKind === SyntaxKind.CatchBlock ||
|
||||
isFunction(containingNodeKind);
|
||||
|
||||
case TypeScript.SyntaxKind.OpenBraceToken:
|
||||
return containingNodeKind === TypeScript.SyntaxKind.EnumDeclaration; // enum { |
|
||||
case SyntaxKind.OpenBraceToken:
|
||||
return containingNodeKind === SyntaxKind.EnumDeclaration || // enum a { |
|
||||
containingNodeKind === SyntaxKind.InterfaceDeclaration; // interface a { |
|
||||
|
||||
case TypeScript.SyntaxKind.PublicKeyword:
|
||||
case TypeScript.SyntaxKind.PrivateKeyword:
|
||||
case TypeScript.SyntaxKind.StaticKeyword:
|
||||
case TypeScript.SyntaxKind.DotDotDotToken:
|
||||
return containingNodeKind === TypeScript.SyntaxKind.Parameter;
|
||||
case SyntaxKind.SemicolonToken:
|
||||
return containingNodeKind === SyntaxKind.Property &&
|
||||
previousToken.parent.parent.kind === SyntaxKind.InterfaceDeclaration; // interface a { f; |
|
||||
|
||||
case TypeScript.SyntaxKind.ClassKeyword:
|
||||
case TypeScript.SyntaxKind.ModuleKeyword:
|
||||
case TypeScript.SyntaxKind.EnumKeyword:
|
||||
case TypeScript.SyntaxKind.InterfaceKeyword:
|
||||
case TypeScript.SyntaxKind.FunctionKeyword:
|
||||
case TypeScript.SyntaxKind.VarKeyword:
|
||||
case TypeScript.SyntaxKind.GetKeyword:
|
||||
case TypeScript.SyntaxKind.SetKeyword:
|
||||
case SyntaxKind.PublicKeyword:
|
||||
case SyntaxKind.PrivateKeyword:
|
||||
case SyntaxKind.StaticKeyword:
|
||||
case SyntaxKind.DotDotDotToken:
|
||||
return containingNodeKind === SyntaxKind.Parameter;
|
||||
|
||||
case SyntaxKind.ClassKeyword:
|
||||
case SyntaxKind.ModuleKeyword:
|
||||
case SyntaxKind.EnumKeyword:
|
||||
case SyntaxKind.InterfaceKeyword:
|
||||
case SyntaxKind.FunctionKeyword:
|
||||
case SyntaxKind.VarKeyword:
|
||||
case SyntaxKind.GetKeyword:
|
||||
case SyntaxKind.SetKeyword:
|
||||
return true;
|
||||
}
|
||||
|
||||
// Previous token may have been a keyword that was converted to an identifier.
|
||||
switch (positionedToken.text()) {
|
||||
switch (previousToken.getText()) {
|
||||
case "class":
|
||||
case "interface":
|
||||
case "enum":
|
||||
case "module":
|
||||
case "function":
|
||||
case "var":
|
||||
// TODO: add let and const
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -2416,44 +2611,15 @@ module ts {
|
||||
return false;
|
||||
}
|
||||
|
||||
function getNonIdentifierCompleteTokenOnLeft(sourceUnit: TypeScript.SourceUnitSyntax, position: number): TypeScript.ISyntaxToken {
|
||||
var positionedToken = TypeScript.Syntax.findCompleteTokenOnLeft(sourceUnit, position, /*includeSkippedTokens*/true);
|
||||
|
||||
if (positionedToken && position === TypeScript.end(positionedToken) && positionedToken.kind() == TypeScript.SyntaxKind.EndOfFileToken) {
|
||||
// EndOfFile token is not interesting, get the one before it
|
||||
positionedToken = TypeScript. previousToken(positionedToken, /*includeSkippedTokens*/true);
|
||||
}
|
||||
|
||||
if (positionedToken && position === TypeScript.end(positionedToken) && positionedToken.kind() === TypeScript.SyntaxKind.IdentifierName) {
|
||||
// The caret is at the end of an identifier, the decision to provide completion depends on the previous token
|
||||
positionedToken = TypeScript.previousToken(positionedToken, /*includeSkippedTokens*/true);
|
||||
}
|
||||
|
||||
return positionedToken;
|
||||
}
|
||||
|
||||
function isRightOfIllegalDot(sourceUnit: TypeScript.SourceUnitSyntax, position: number): boolean {
|
||||
var positionedToken = getNonIdentifierCompleteTokenOnLeft(sourceUnit, position);
|
||||
|
||||
if (positionedToken) {
|
||||
switch (positionedToken.kind()) {
|
||||
case TypeScript.SyntaxKind.DotToken:
|
||||
var leftOfDotPositionedToken = TypeScript.previousToken(positionedToken, /*includeSkippedTokens*/true);
|
||||
return leftOfDotPositionedToken && leftOfDotPositionedToken.kind() === TypeScript.SyntaxKind.NumericLiteral;
|
||||
|
||||
case TypeScript.SyntaxKind.NumericLiteral:
|
||||
var text = positionedToken.text();
|
||||
return text.charAt(text.length - 1) === ".";
|
||||
}
|
||||
function isRightOfIllegalDot(previousToken: Node): boolean {
|
||||
if (previousToken && previousToken.kind === SyntaxKind.NumericLiteral) {
|
||||
var text = previousToken.getFullText();
|
||||
return text.charAt(text.length - 1) === ".";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function isPunctuation(kind: SyntaxKind) {
|
||||
return (SyntaxKind.FirstPunctuation <= kind && kind <= SyntaxKind.LastPunctuation);
|
||||
}
|
||||
|
||||
function filterContextualMembersList(contextualMemberSymbols: Symbol[], existingMembers: Declaration[]): Symbol[] {
|
||||
if (!existingMembers || existingMembers.length === 0) {
|
||||
return contextualMemberSymbols;
|
||||
@@ -2483,162 +2649,6 @@ module ts {
|
||||
|
||||
return filteredMembers;
|
||||
}
|
||||
|
||||
synchronizeHostData();
|
||||
|
||||
filename = TypeScript.switchToForwardSlashes(filename);
|
||||
|
||||
var sourceFile = getSourceFile(filename);
|
||||
var sourceUnit = sourceFile.getSourceUnit();
|
||||
|
||||
if (isCompletionListBlocker(sourceFile.getSyntaxTree().sourceUnit(), position)) {
|
||||
host.log("Returning an empty list because completion was blocked.");
|
||||
return null;
|
||||
}
|
||||
|
||||
var node = TypeScript.ASTHelpers.getAstAtPosition(sourceUnit, position, /*useTrailingTriviaAsLimChar*/ true, /*forceInclusive*/ true);
|
||||
|
||||
if (node && node.kind() === TypeScript.SyntaxKind.IdentifierName &&
|
||||
TypeScript.start(node) === TypeScript.end(node)) {
|
||||
// Ignore missing name nodes
|
||||
node = node.parent;
|
||||
}
|
||||
|
||||
var isRightOfDot = false;
|
||||
if (node &&
|
||||
node.kind() === TypeScript.SyntaxKind.MemberAccessExpression &&
|
||||
TypeScript.end((<TypeScript.MemberAccessExpressionSyntax>node).expression) < position) {
|
||||
|
||||
isRightOfDot = true;
|
||||
node = (<TypeScript.MemberAccessExpressionSyntax>node).expression;
|
||||
}
|
||||
else if (node &&
|
||||
node.kind() === TypeScript.SyntaxKind.QualifiedName &&
|
||||
TypeScript.end((<TypeScript.QualifiedNameSyntax>node).left) < position) {
|
||||
|
||||
isRightOfDot = true;
|
||||
node = (<TypeScript.QualifiedNameSyntax>node).left;
|
||||
}
|
||||
else if (node && node.parent &&
|
||||
node.kind() === TypeScript.SyntaxKind.IdentifierName &&
|
||||
node.parent.kind() === TypeScript.SyntaxKind.MemberAccessExpression &&
|
||||
(<TypeScript.MemberAccessExpressionSyntax>node.parent).name === node) {
|
||||
|
||||
isRightOfDot = true;
|
||||
node = (<TypeScript.MemberAccessExpressionSyntax>node.parent).expression;
|
||||
}
|
||||
else if (node && node.parent &&
|
||||
node.kind() === TypeScript.SyntaxKind.IdentifierName &&
|
||||
node.parent.kind() === TypeScript.SyntaxKind.QualifiedName &&
|
||||
(<TypeScript.QualifiedNameSyntax>node.parent).right === node) {
|
||||
|
||||
isRightOfDot = true;
|
||||
node = (<TypeScript.QualifiedNameSyntax>node.parent).left;
|
||||
}
|
||||
|
||||
// TODO: this is a hack for now, we need a proper walking mechanism to verify that we have the correct node
|
||||
var precedingToken = findTokenOnLeftOfPosition(sourceFile, TypeScript.end(node));
|
||||
var mappedNode: Node;
|
||||
if (!precedingToken) {
|
||||
mappedNode = sourceFile;
|
||||
}
|
||||
else if (isPunctuation(precedingToken.kind)) {
|
||||
mappedNode = precedingToken.parent;
|
||||
}
|
||||
else {
|
||||
mappedNode = precedingToken;
|
||||
}
|
||||
|
||||
Debug.assert(mappedNode, "Could not map a Fidelity node to an AST node");
|
||||
|
||||
// Get the completions
|
||||
activeCompletionSession = {
|
||||
filename: filename,
|
||||
position: position,
|
||||
entries: [],
|
||||
symbols: {},
|
||||
location: mappedNode,
|
||||
typeChecker: typeInfoResolver
|
||||
};
|
||||
|
||||
// Right of dot member completion list
|
||||
if (isRightOfDot) {
|
||||
var symbols: Symbol[] = [];
|
||||
isMemberCompletion = true;
|
||||
|
||||
if (mappedNode.kind === SyntaxKind.Identifier || mappedNode.kind === SyntaxKind.QualifiedName || mappedNode.kind === SyntaxKind.PropertyAccess) {
|
||||
var symbol = typeInfoResolver.getSymbolInfo(mappedNode);
|
||||
|
||||
// This is an alias, follow what it aliases
|
||||
if (symbol && symbol.flags & SymbolFlags.Import) {
|
||||
symbol = typeInfoResolver.getAliasedSymbol(symbol);
|
||||
}
|
||||
|
||||
if (symbol && symbol.flags & SymbolFlags.HasExports) {
|
||||
// Extract module or enum members
|
||||
forEachValue(symbol.exports, symbol => {
|
||||
if (typeInfoResolver.isValidPropertyAccess(<PropertyAccess>(mappedNode.parent), symbol.name)) {
|
||||
symbols.push(symbol);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var type = typeInfoResolver.getTypeOfNode(mappedNode);
|
||||
if (type) {
|
||||
// Filter private properties
|
||||
forEach(type.getApparentProperties(), symbol => {
|
||||
if (typeInfoResolver.isValidPropertyAccess(<PropertyAccess>(mappedNode.parent), symbol.name)) {
|
||||
symbols.push(symbol);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getCompletionEntriesFromSymbols(symbols, activeCompletionSession);
|
||||
}
|
||||
else {
|
||||
var containingObjectLiteral = getContainingObjectLiteralApplicableForCompletion(sourceFile.getSyntaxTree().sourceUnit(), position);
|
||||
|
||||
// Object literal expression, look up possible property names from contextual type
|
||||
if (containingObjectLiteral) {
|
||||
var objectLiteral = <ObjectLiteral>(mappedNode.kind === SyntaxKind.ObjectLiteral ? mappedNode : getAncestor(mappedNode, SyntaxKind.ObjectLiteral));
|
||||
|
||||
Debug.assert(objectLiteral);
|
||||
|
||||
isMemberCompletion = true;
|
||||
|
||||
var contextualType = typeInfoResolver.getContextualType(objectLiteral);
|
||||
if (!contextualType) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var contextualTypeMembers = typeInfoResolver.getPropertiesOfType(contextualType);
|
||||
if (contextualTypeMembers && contextualTypeMembers.length > 0) {
|
||||
// Add filtered items to the completion list
|
||||
var filteredMembers = filterContextualMembersList(contextualTypeMembers, objectLiteral.properties);
|
||||
getCompletionEntriesFromSymbols(filteredMembers, activeCompletionSession);
|
||||
}
|
||||
}
|
||||
// Get scope members
|
||||
else {
|
||||
isMemberCompletion = false;
|
||||
/// TODO filter meaning based on the current context
|
||||
var symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Import;
|
||||
var symbols = typeInfoResolver.getSymbolsInScope(mappedNode, symbolMeanings);
|
||||
|
||||
getCompletionEntriesFromSymbols(symbols, activeCompletionSession);
|
||||
}
|
||||
}
|
||||
|
||||
// Add keywords if this is not a member completion list
|
||||
if (!isMemberCompletion) {
|
||||
Array.prototype.push.apply(activeCompletionSession.entries, keywordCompletions);
|
||||
}
|
||||
|
||||
return {
|
||||
isMemberCompletion: isMemberCompletion,
|
||||
entries: activeCompletionSession.entries
|
||||
};
|
||||
}
|
||||
|
||||
function getCompletionEntryDetails(filename: string, position: number, entryName: string): CompletionEntryDetails {
|
||||
@@ -2646,6 +2656,8 @@ module ts {
|
||||
// in the getCompletionsAtPosition earlier
|
||||
filename = TypeScript.switchToForwardSlashes(filename);
|
||||
|
||||
var sourceFile = getSourceFile(filename);
|
||||
|
||||
var session = activeCompletionSession;
|
||||
|
||||
// Ensure that the current active completion session is still valid for this request
|
||||
@@ -2662,7 +2674,8 @@ module ts {
|
||||
// which is permissible given that it is backwards compatible; but really we should consider
|
||||
// passing the meaning for the node so that we don't report that a suggestion for a value is an interface.
|
||||
// We COULD also just do what 'getSymbolModifiers' does, which is to use the first declaration.
|
||||
var displayPartsDocumentationsAndSymbolKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getSourceFile(filename), session.location, session.typeChecker, session.location, SemanticMeaning.All);
|
||||
var location = getTouchingPropertyName(sourceFile, position);
|
||||
var displayPartsDocumentationsAndSymbolKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getSourceFile(filename), location, session.typeChecker, location, SemanticMeaning.All);
|
||||
return {
|
||||
name: entryName,
|
||||
kind: displayPartsDocumentationsAndSymbolKind.symbolKind,
|
||||
@@ -2824,14 +2837,24 @@ module ts {
|
||||
|
||||
var type = typeResolver.getTypeOfSymbol(symbol);
|
||||
if (type) {
|
||||
if (isCallExpressionTarget(location) || isNewExpressionTarget(location)) {
|
||||
// try get the call/construct signature from the type if it matches
|
||||
var callExpression: CallExpression;
|
||||
if (location.parent.kind === SyntaxKind.PropertyAccess && (<PropertyAccess>location.parent).right === location) {
|
||||
if (location.parent && location.parent.kind === SyntaxKind.PropertyAccess) {
|
||||
var right = (<PropertyAccess>location.parent).right;
|
||||
// Either the location is on the right of a property access, or on the left and the right is missing
|
||||
if (right === location || (right && right.kind === SyntaxKind.Missing)){
|
||||
location = location.parent;
|
||||
}
|
||||
callExpression = <CallExpression>location.parent;
|
||||
}
|
||||
|
||||
// try get the call/construct signature from the type if it matches
|
||||
var callExpression: CallExpression;
|
||||
if (location.kind === SyntaxKind.CallExpression || location.kind === SyntaxKind.NewExpression) {
|
||||
callExpression = <CallExpression> location;
|
||||
}
|
||||
else if (isCallExpressionTarget(location) || isNewExpressionTarget(location)) {
|
||||
callExpression = <CallExpression>location.parent;
|
||||
}
|
||||
|
||||
if (callExpression) {
|
||||
var candidateSignatures: Signature[] = [];
|
||||
signature = typeResolver.getResolvedSignature(callExpression, candidateSignatures);
|
||||
if (!signature && candidateSignatures.length) {
|
||||
@@ -5132,16 +5155,7 @@ module ts {
|
||||
// OK, we have found a match in the file. This is only an acceptable match if
|
||||
// it is contained within a comment.
|
||||
var token = getTokenAtPosition(sourceFile, matchPosition);
|
||||
|
||||
if (token.getStart() <= matchPosition && matchPosition < token.getEnd()) {
|
||||
// match was within the token itself. Not in the comment. Keep searching
|
||||
// descriptor.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Looks to be within the trivia. See if we can find the comment containing it.
|
||||
if (!getContainingComment(getTrailingCommentRanges(fileContents, token.getFullStart()), matchPosition) &&
|
||||
!getContainingComment(getLeadingCommentRanges(fileContents, token.getFullStart()), matchPosition)) {
|
||||
if (!isInsideComment(sourceFile, token, matchPosition)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -95,11 +95,12 @@ module ts {
|
||||
var child = current.getChildAt(i);
|
||||
var start = allowPositionInLeadingTrivia ? child.getFullStart() : child.getStart(sourceFile);
|
||||
if (start <= position) {
|
||||
if (position < child.getEnd()) {
|
||||
var end = child.getEnd();
|
||||
if (position < end || (position === end && child.kind === SyntaxKind.EndOfFileToken)) {
|
||||
current = child;
|
||||
continue outer;
|
||||
}
|
||||
else if (includeItemAtEndPosition && child.getEnd() === position) {
|
||||
else if (includeItemAtEndPosition && end === position) {
|
||||
var previousToken = findPrecedingToken(position, sourceFile, child);
|
||||
if (previousToken && includeItemAtEndPosition(previousToken)) {
|
||||
return previousToken;
|
||||
@@ -180,7 +181,7 @@ module ts {
|
||||
for (var i = 0, len = children.length; i < len; ++i) {
|
||||
var child = children[i];
|
||||
if (nodeHasTokens(child)) {
|
||||
if (position < child.end) {
|
||||
if (position <= child.end) {
|
||||
if (child.getStart(sourceFile) >= position) {
|
||||
// actual start of the node is past the position - previous token should be at the end of previous child
|
||||
var candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ i);
|
||||
|
||||
Reference in New Issue
Block a user