Exemplo n.º 1
0
        public static void OnRemovedText(FGTextBuffer buffer, FGTextBuffer.CaretPos fromPos, FGTextBuffer.CaretPos toPos)
        {
            var guid = buffer.guid;
            var from = new TextPosition(fromPos.line, fromPos.characterIndex);
            var to = new TextPosition(toPos.line, toPos.characterIndex);

            var recentLocations = _instance.recentLocations;
            for (var i = recentLocations.Count; i --> 0; )
            {
            var entry = recentLocations[i];
            if (entry.assetGuid != guid)
                continue;

            var location = new TextPosition(entry.line, entry.index);
            if (from >= location)
                continue;

            if (to >= location)
            {
                entry.line = from.line;
                entry.index = from.index;
                continue;
            }

            if (to.line == location.line)
                entry.index -= to.index - from.index;
            entry.line -= to.line - from.line;
            }

            if (recentLocations.Count > 1)
            {
            var prevLocation = recentLocations[0];
            for (var i = 1; i < recentLocations.Count; ++i)
            {
                var entry = recentLocations[i];
                if (entry.assetGuid != prevLocation.assetGuid || entry.line != prevLocation.line || entry.index != prevLocation.index)
                {
                    prevLocation = entry;
                }
                else
                {
                    if (_instance.recentLocationsOffset >= recentLocations.Count - i)
                        --_instance.recentLocationsOffset;
                    recentLocations.RemoveAt(i);
                    --i;
                }
            }
            }
        }
Exemplo n.º 2
0
	public bool Contains(TextPosition position)
	{
		return !(position.line < line
			|| position.line == line && (position.index < index || lineOffset == 0 && position.index > index + indexOffset)
			|| position.line > line + lineOffset
			|| position.line == line + lineOffset && position.index > indexOffset);
	}
Exemplo n.º 3
0
        public static void OnInsertedText(FGTextBuffer buffer, FGTextBuffer.CaretPos fromPos, FGTextBuffer.CaretPos toPos)
        {
            var guid = buffer.guid;
            var from = new TextPosition(fromPos.line, fromPos.characterIndex);
            var to = new TextPosition(toPos.line, toPos.characterIndex);

            var recentLocations = _instance.recentLocations;
            for (var i = recentLocations.Count; i --> 0; )
            {
            var entry = recentLocations[i];
            if (entry.assetGuid != guid)
                continue;

            var location = new TextPosition(entry.line, entry.index);
            if (from > location || insertingTextAfterCaret && from == location)
                continue;

            if (from.line == location.line)
                entry.index += to.index - from.index;
            entry.line += to.line - from.line;
            }
        }
Exemplo n.º 4
0
	public static TextSpan Create(TextPosition from, TextPosition to)
	{
		return new TextSpan
		{
			line = from.line,
			index = from.index,
			lineOffset = to.line - from.line,
			indexOffset = to.index - (to.line == from.line ? from.index : 0)
		};
	}
Exemplo n.º 5
0
	public static TextSpan Create(TextPosition start, TextOffset length)
	{
		return new TextSpan
		{
			line = start.line,
			index = start.index,
			lineOffset = length.lines,
			indexOffset = length.indexOffset
		};
	}
Exemplo n.º 6
0
	public static TextSpan CreateEmpty(TextPosition position)
	{
		return new TextSpan { line = position.line, index = position.index };
	}
Exemplo n.º 7
0
	static FindResultsWindow.ResultType ValidateResultAsReference(string assetGuid, TextPosition location, int length, ref SymbolDefinition referencedSymbol)
	{
		var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
		if (string.IsNullOrEmpty(assetPath))
			return FindResultsWindow.ResultType.RemoveResult;
		
		var isCsScript = assetPath.EndsWith(".cs", System.StringComparison.OrdinalIgnoreCase);
		
		var buffer = FGTextBufferManager.GetBuffer(assetGuid);
		if (buffer == null)
			return FindResultsWindow.ResultType.RemoveResult;
		
		if (buffer.Parser == null)
		{
			buffer.LoadImmediately();
			referencedSymbol = referencedSymbol.Rebind();
		}
		
		var formatedLine = buffer.formatedLines[location.line];
		
		var textLine = buffer.lines[location.line];
		var isVarResult = length == 3 && (referencedSymbol is TypeDefinitionBase) && textLine.Substring(location.index, length) == "var";
		
		if (isCsScript)
		{
			if (formatedLine.regionTree.kind > FGTextBuffer.RegionTree.Kind.LastActive)
			{
				if (isVarResult)
					return FindResultsWindow.ResultType.RemoveResult;
				return FindResultsWindow.ResultType.InactiveCode;
			}
		}
		else if (isVarResult)
		{
			return FindResultsWindow.ResultType.RemoveResult;
		}
		
		int tokenIndex;
		bool atTokenEnd;
		var token = buffer.GetTokenAt(new TextPosition(location.line, location.index + 1), out location.line, out tokenIndex, out atTokenEnd);
		switch (token.tokenKind)
		{
			case SyntaxToken.Kind.Preprocessor:
				return FindResultsWindow.ResultType.RemoveResult;
				
			case SyntaxToken.Kind.Comment:
			case SyntaxToken.Kind.PreprocessorArguments:
			case SyntaxToken.Kind.PreprocessorSymbol:
				if (isVarResult)
					return FindResultsWindow.ResultType.RemoveResult;
				return FindResultsWindow.ResultType.Comment;
			
			case SyntaxToken.Kind.StringLiteral:
			case SyntaxToken.Kind.VerbatimStringLiteral:
				if (isVarResult)
					return FindResultsWindow.ResultType.RemoveResult;
				return FindResultsWindow.ResultType.String;
		}
		
		if (!isCsScript || token.parent == null)
			return FindResultsWindow.ResultType.UnresolvedSymbol;
		
		var resolvedSymbol = token.parent.resolvedSymbol;
		if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
			FGResolver.ResolveNode(token.parent.parent);
		
		if (resolvedSymbol != null && resolvedSymbol.kind == SymbolKind.MethodGroup && token.parent.parent != null)
		{
			var nextLeaf = token.parent.parent.FindNextLeaf();
			if (nextLeaf != null && nextLeaf.IsLit("("))
			{
				var nextNode = nextLeaf.parent;
				if (nextNode.RuleName == "arguments")
				{
					FGResolver.ResolveNode(nextNode);
					if (token.parent != null)
						if (token.parent.resolvedSymbol == null || token.parent.resolvedSymbol.kind == SymbolKind.Error)
							token.parent.resolvedSymbol = resolvedSymbol;
				}
			}
		}
		
		resolvedSymbol = token.parent != null ? token.parent.resolvedSymbol : null;
		if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
			return FindResultsWindow.ResultType.UnresolvedSymbol;
		
		if (resolvedSymbol.kind == SymbolKind.Constructor || resolvedSymbol.kind == SymbolKind.Destructor)
			resolvedSymbol = resolvedSymbol.parentSymbol;
		if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
			return FindResultsWindow.ResultType.UnresolvedSymbol;
		
		var constructedSymbol = resolvedSymbol;
		resolvedSymbol = resolvedSymbol.GetGenericSymbol();
		
		if (referencedSymbol.kind == SymbolKind.MethodGroup && resolvedSymbol.kind == SymbolKind.Method)
			resolvedSymbol = resolvedSymbol.parentSymbol;
		
		if (resolvedSymbol != referencedSymbol)
		{
			var typeArgument = referencedSymbol as TypeDefinitionBase;
			var constructedType = constructedSymbol as ConstructedTypeDefinition;
			if (isVarResult && typeArgument != null && constructedType != null)
				if (IsUsedAsTypeArgument(typeArgument.GetGenericSymbol() as TypeDefinitionBase, constructedType))
					return FindResultsWindow.ResultType.VarTemplateReference;
			
			if (resolvedSymbol.kind == SymbolKind.Method && referencedSymbol.kind == SymbolKind.Method)
			{
				if (resolvedSymbol.parentSymbol == referencedSymbol.parentSymbol)
					return FindResultsWindow.ResultType.MethodOverload;
				
				var resolvedMethod = resolvedSymbol as MethodDefinition;
				var referencedMethod = referencedSymbol as MethodDefinition;
				if (resolvedMethod != null && referencedMethod != null)
				{
					var resolvedType = resolvedMethod.parentSymbol as TypeDefinitionBase
						?? resolvedMethod.parentSymbol.parentSymbol as TypeDefinitionBase;
					var referencedType = referencedMethod.parentSymbol as TypeDefinitionBase
						?? referencedMethod.parentSymbol.parentSymbol as TypeDefinitionBase;
					
					var isInterface = resolvedType.kind == SymbolKind.Interface || referencedType.kind == SymbolKind.Interface;
					
					var resolvedIsVirtual = isInterface || resolvedMethod.IsOverride || resolvedMethod.IsVirtual || resolvedMethod.IsAbstract;
					var referencedIsVirtual = isInterface || referencedMethod.IsOverride || referencedMethod.IsVirtual || referencedMethod.IsAbstract;
					if (resolvedIsVirtual && referencedIsVirtual)
					{
						if (System.Linq.Enumerable.SequenceEqual(
							System.Linq.Enumerable.Select(resolvedMethod.GetParameters(), x => x.TypeOf()),
							System.Linq.Enumerable.Select(referencedMethod.GetParameters(), x => x.TypeOf()) ))
						{
							if (resolvedType.DerivesFrom(referencedType))
								return FindResultsWindow.ResultType.OverridingMethod;
							if (referencedType.DerivesFrom(resolvedType))
								return FindResultsWindow.ResultType.OverriddenMethod;
						}
					}
				}
			}

			if (resolvedSymbol.kind != SymbolKind.MethodGroup || referencedSymbol.parentSymbol != resolvedSymbol)
				return FindResultsWindow.ResultType.RemoveResult;
		}
		
		if (isVarResult)
			return FindResultsWindow.ResultType.VarReference;
		
		if (FGResolver.IsWriteReference(token))
			return FindResultsWindow.ResultType.WriteReference;
		return FindResultsWindow.ResultType.ReadReference;
	}
	private void AddResult(string text, string guid, TextPosition location, int length)
	{
		try
		{
			var resultType = ResultType.Default;
			if (validateResultFunction != null)
			{
				resultType = validateResultFunction(guid, location, length, ref referencedSymbol);
				if (resultType == ResultType.RemoveResult)
					return;
			}
			
			bool visible = CheckFiltering(resultType);
			
			//resultsLock.EnterWriteLock();
			var trim = !char.IsWhiteSpace(text, location.index) && !char.IsWhiteSpace(text, location.index + length - 1);
			
			if (visible)
			{
				var lastAssetGuid = results.Count > 0 ? results[results.Count - 1].assetGuid : null;
				if (guid != lastAssetGuid)
				{
					++filesCount;
					
					if (GroupByFile)
					{
						results.Add(
							new FoundResult {
							assetGuid = guid,
							assetPath = AssetDatabase.GUIDToAssetPath(guid),
							selected = true,
						});
					}
				}
				else if (lastAssetGuid == null)
				{
					++filesCount;
				}
			}
			
			var trimmed = trim ? text.TrimStart() : text;
			var trimOffset = text.Length - trimmed.Length;
			trimmed = trim ? trimmed.TrimEnd() : text;
			var assetPath = AssetDatabase.GUIDToAssetPath(guid);
			var newFindResult =
				new FoundResult {
					description = trimmed,
					assetGuid = guid,
					assetPath = assetPath,
					fileName = System.IO.Path.GetFileName(assetPath),
					line = location.line,
					characterIndex = location.index,
					length = length,
					trimOffset = trimOffset,
					resultType = resultType,
					selected = true,
				};
			if (visible)
			{
				results.Add(newFindResult);
				++resultsCount;
				resultsCountText = "Found " + resultsCount + " results in " + filesCount + " files.";
			}
			flatResults.Add(newFindResult);
		}
		finally
		{
			//resultsLock.ExitWriteLock();
		}
		
		repaintOnUpdate = true;
	}
Exemplo n.º 9
0
        static FindResultsWindow.ResultType ValidateResultAsReference(string assetGuid, TextPosition location, int length, ref SymbolDefinition referencedSymbol)
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);

            if (string.IsNullOrEmpty(assetPath))
            {
                return(FindResultsWindow.ResultType.RemoveResult);
            }

            var isCsScript = assetPath.EndsWith(".cs", System.StringComparison.OrdinalIgnoreCase);

            var buffer = FGTextBufferManager.GetBuffer(assetGuid);

            if (buffer == null)
            {
                return(FindResultsWindow.ResultType.RemoveResult);
            }

            if (buffer.Parser == null)
            {
                buffer.LoadImmediately();
                referencedSymbol = referencedSymbol.Rebind();
            }

            var formatedLine = buffer.formatedLines[location.line];

            var textLine    = buffer.lines[location.line];
            var isVarResult =
                length == 3 &&
                referencedSymbol is TypeDefinitionBase &&
                location.index + 3 < textLine.Length &&
                textLine[location.index] == 'v' &&
                textLine[location.index + 1] == 'a' &&
                textLine[location.index + 2] == 'r';

            if (isCsScript)
            {
                if (formatedLine.regionTree.kind > FGTextBuffer.RegionTree.Kind.LastActive)
                {
                    if (isVarResult)
                    {
                        return(FindResultsWindow.ResultType.RemoveResult);
                    }
                    return(FindResultsWindow.ResultType.InactiveCode);
                }
            }
            else if (isVarResult)
            {
                return(FindResultsWindow.ResultType.RemoveResult);
            }

            int  tokenIndex;
            bool atTokenEnd;
            var  token = buffer.GetTokenAt(new TextPosition(location.line, location.index + 1), out location.line, out tokenIndex, out atTokenEnd);

            switch (token.tokenKind)
            {
            case SyntaxToken.Kind.Preprocessor:
                return(FindResultsWindow.ResultType.RemoveResult);

            case SyntaxToken.Kind.Comment:
            case SyntaxToken.Kind.PreprocessorArguments:
            case SyntaxToken.Kind.PreprocessorSymbol:
                if (isVarResult)
                {
                    return(FindResultsWindow.ResultType.RemoveResult);
                }
                return(FindResultsWindow.ResultType.Comment);

            case SyntaxToken.Kind.StringLiteral:
            case SyntaxToken.Kind.VerbatimStringLiteral:
                if (isVarResult)
                {
                    return(FindResultsWindow.ResultType.RemoveResult);
                }
                return(FindResultsWindow.ResultType.String);
            }

            if (!isCsScript || token.parent == null)
            {
                return(isVarResult ? FindResultsWindow.ResultType.UnresolvedVarSymbol : FindResultsWindow.ResultType.UnresolvedSymbol);
            }

            var resolvedSymbol = token.parent.resolvedSymbol;

            if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
            {
                FGResolver.ResolveNode(token.parent.parent);
            }

            if (resolvedSymbol != null && resolvedSymbol.kind == SymbolKind.MethodGroup && token.parent.parent != null)
            {
                var nextLeaf = token.parent.parent.FindNextLeaf();
                if (nextLeaf != null && nextLeaf.IsLit("("))
                {
                    var nextNode = nextLeaf.parent;
                    if (nextNode.RuleName == "arguments")
                    {
                        FGResolver.ResolveNode(nextNode);
                        if (token.parent != null)
                        {
                            if (token.parent.resolvedSymbol == null || token.parent.resolvedSymbol.kind == SymbolKind.Error)
                            {
                                token.parent.resolvedSymbol = resolvedSymbol;
                            }
                        }
                    }
                }
            }

            resolvedSymbol = token.parent != null ? token.parent.resolvedSymbol : null;
            if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
            {
                return(isVarResult ? FindResultsWindow.ResultType.UnresolvedVarSymbol : FindResultsWindow.ResultType.UnresolvedSymbol);
            }

            if (resolvedSymbol.kind == SymbolKind.Constructor || resolvedSymbol.kind == SymbolKind.Destructor)
            {
                resolvedSymbol = resolvedSymbol.parentSymbol;
            }
            if (resolvedSymbol == null || resolvedSymbol.kind == SymbolKind.Error)
            {
                return(isVarResult ? FindResultsWindow.ResultType.UnresolvedVarSymbol : FindResultsWindow.ResultType.UnresolvedSymbol);
            }

            var constructedSymbol = resolvedSymbol;

            resolvedSymbol = resolvedSymbol.GetGenericSymbol();

            if (referencedSymbol.kind == SymbolKind.MethodGroup && resolvedSymbol.kind == SymbolKind.Method)
            {
                resolvedSymbol = resolvedSymbol.parentSymbol;
            }

            if (resolvedSymbol != referencedSymbol)
            {
                var typeArgument    = referencedSymbol as TypeDefinitionBase;
                var constructedType = constructedSymbol as ConstructedTypeDefinition;
                if (isVarResult && typeArgument != null && constructedType != null)
                {
                    if (IsUsedAsTypeArgument(typeArgument.GetGenericSymbol() as TypeDefinitionBase, constructedType))
                    {
                        return(FindResultsWindow.ResultType.VarTemplateReference);
                    }
                }

                if (resolvedSymbol.kind == SymbolKind.Property && referencedSymbol.kind == SymbolKind.Property ||
                    resolvedSymbol.kind == SymbolKind.Event && referencedSymbol.kind == SymbolKind.Event ||
                    resolvedSymbol.kind == SymbolKind.Indexer && referencedSymbol.kind == SymbolKind.Indexer)
                {
                    var resolvedProperty   = resolvedSymbol as InstanceDefinition;
                    var referencedProperty = referencedSymbol as InstanceDefinition;
                    if (resolvedProperty != null && referencedProperty != null)
                    {
                        var resolvedType = resolvedProperty.parentSymbol as TypeDefinitionBase
                                           ?? resolvedProperty.parentSymbol.parentSymbol as TypeDefinitionBase;
                        var referencedType = referencedProperty.parentSymbol as TypeDefinitionBase
                                             ?? referencedProperty.parentSymbol.parentSymbol as TypeDefinitionBase;

                        var isInterface = resolvedType != null && resolvedType.kind == SymbolKind.Interface ||
                                          referencedType != null && referencedType.kind == SymbolKind.Interface;

                        var resolvedIsVirtual   = isInterface || resolvedProperty.IsOverride || resolvedProperty.IsVirtual || resolvedProperty.IsAbstract;
                        var referencedIsVirtual = isInterface || referencedProperty.IsOverride || referencedProperty.IsVirtual || referencedProperty.IsAbstract;
                        if (resolvedIsVirtual && referencedIsVirtual)
                        {
                            if (resolvedSymbol.kind != SymbolKind.Indexer ||
                                System.Linq.Enumerable.SequenceEqual(
                                    System.Linq.Enumerable.Select(resolvedProperty.GetParameters(), x => x.TypeOf()),
                                    System.Linq.Enumerable.Select(referencedProperty.GetParameters(), x => x.TypeOf())))
                            {
                                if (resolvedType != null && resolvedType.DerivesFrom(referencedType))
                                {
                                    return(FindResultsWindow.ResultType.OverridingMethod);
                                }
                                if (referencedType != null && referencedType.DerivesFrom(resolvedType))
                                {
                                    return(FindResultsWindow.ResultType.OverriddenMethod);
                                }
                            }
                        }
                    }
                }

                if (resolvedSymbol.kind == SymbolKind.Method && referencedSymbol.kind == SymbolKind.Method)
                {
                    if (resolvedSymbol.parentSymbol == referencedSymbol.parentSymbol)
                    {
                        return(FindResultsWindow.ResultType.MethodOverload);
                    }

                    var resolvedMethod   = resolvedSymbol as MethodDefinition;
                    var referencedMethod = referencedSymbol as MethodDefinition;
                    if (resolvedMethod != null && referencedMethod != null)
                    {
                        var resolvedType = resolvedMethod.parentSymbol as TypeDefinitionBase
                                           ?? resolvedMethod.parentSymbol.parentSymbol as TypeDefinitionBase;
                        var referencedType = referencedMethod.parentSymbol as TypeDefinitionBase
                                             ?? referencedMethod.parentSymbol.parentSymbol as TypeDefinitionBase;

                        var isInterface = resolvedType != null && resolvedType.kind == SymbolKind.Interface || referencedType != null && referencedType.kind == SymbolKind.Interface;

                        var resolvedIsVirtual   = isInterface || resolvedMethod.IsOverride || resolvedMethod.IsVirtual || resolvedMethod.IsAbstract;
                        var referencedIsVirtual = isInterface || referencedMethod.IsOverride || referencedMethod.IsVirtual || referencedMethod.IsAbstract;
                        if (resolvedIsVirtual && referencedIsVirtual)
                        {
                            if (System.Linq.Enumerable.SequenceEqual(
                                    System.Linq.Enumerable.Select(resolvedMethod.GetParameters(), x => x.TypeOf()),
                                    System.Linq.Enumerable.Select(referencedMethod.GetParameters(), x => x.TypeOf())))
                            {
                                if (resolvedType != null && resolvedType.DerivesFrom(referencedType))
                                {
                                    return(FindResultsWindow.ResultType.OverridingMethod);
                                }
                                if (referencedType != null && referencedType.DerivesFrom(resolvedType))
                                {
                                    return(FindResultsWindow.ResultType.OverriddenMethod);
                                }
                            }
                        }
                    }
                }

                if (resolvedSymbol.kind != SymbolKind.MethodGroup || referencedSymbol.parentSymbol != resolvedSymbol)
                {
                    return(FindResultsWindow.ResultType.RemoveResult);
                }
            }

            if (isVarResult)
            {
                return(FindResultsWindow.ResultType.VarReference);
            }

            if (FGResolver.IsWriteReference(token))
            {
                return(FindResultsWindow.ResultType.WriteReference);
            }
            return(FindResultsWindow.ResultType.ReadReference);
        }
Exemplo n.º 10
0
 public static TextSpan CreateEmpty(TextPosition position)
 {
     return(new TextSpan {
         line = position.line, index = position.index
     });
 }