public ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped) { // We can return code-completion items like this: //return new ICompletionData[] { // new DefaultCompletionData("Text", "Description", 1) //}; NRefactoryResolver resolver; if (mainForm.Language == "C#") { resolver = new NRefactoryResolver(mainForm.myProjectContent, ICSharpCode.SharpDevelop.Dom.LanguageProperties.CSharp); } else { resolver = new NRefactoryResolver(mainForm.myProjectContent, ICSharpCode.SharpDevelop.Dom.LanguageProperties.VBNet); } Dom.ResolveResult rr = resolver.Resolve(FindExpression(textArea), textArea.Caret.Line, textArea.Caret.Column, fileName, textArea.MotherTextEditorControl.Text); List <ICompletionData> resultList = new List <ICompletionData>(); if (rr != null) { ArrayList completionData = rr.GetCompletionData(mainForm.myProjectContent); if (completionData != null) { AddCompletionData(resultList, completionData); } } return(resultList.ToArray()); }
void MakeTypeResult(IClass c) { if (c != null) resolveResult = new TypeResolveResult(callingClass, resolver.CallingMember, c); else ClearResult(); }
void MakeTypeResult(IReturnType rt) { if (rt != null) resolveResult = new TypeResolveResult(callingClass, resolver.CallingMember, rt); else ClearResult(); }
public ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped) { // We can return code-completion items like this: //return new ICompletionData[] { // new DefaultCompletionData("Text", "Description", 1) //}; NRefactoryResolver resolver = new NRefactoryResolver(iForm.ProjectContent.Language); Dom.ResolveResult rr = resolver.Resolve(FindExpression(textArea), iForm.ParseInformation, textArea.MotherTextEditorControl.Text); List <ICompletionData> resultList = new List <ICompletionData>(); if (rr != null) { var completionData = rr.GetCompletionData(iForm.ProjectContent); if (completionData != null) { AddCompletionData(resultList, completionData); } } return(resultList.ToArray()); }
Variable ResolveVariable(Variable variable) { Dom.ParseInformation info = ParserService.GetParseInformation(this.textEditor.FileName); Dom.ExpressionResult res = new Dom.ExpressionResult(variable.Name, Dom.DomRegion.FromLocation(variable.StartPos, variable.EndPos), Dom.ExpressionContext.Default, null); Dom.ResolveResult result = this.GetResolver().Resolve(res, info, this.textEditor.Document.Text); Dom.IReturnType type = currentProjectContent.SystemTypes.Object; Dom.ClassFinder finder = new Dom.ClassFinder(currentClass, textEditor.Caret.Line, textEditor.Caret.Column); if (result != null && result.ResolvedType != null) { type = result.ResolvedType; } if (variable.Type.Type == "var") { variable.Type = Dom.Refactoring.CodeGenerator.ConvertType(type, finder); } variable.IsReferenceType = type.IsReferenceType == true; return(variable); }
void MakeResult(IReturnType type) { if (type == null) ClearResult(); else resolveResult = new ResolveResult(callingClass, resolver.CallingMember, type); }
protected override void RunImpl(ITextEditor editor, int offset, ResolveResult symbol) { if (symbol == null) return; FilePosition pos = symbol.GetDefinitionPosition(); if (pos.IsEmpty) { IEntity entity; if (symbol is MemberResolveResult) { entity = ((MemberResolveResult)symbol).ResolvedMember; } else if (symbol is TypeResolveResult) { entity = ((TypeResolveResult)symbol).ResolvedClass; } else { entity = null; } if (entity != null) { NavigationService.NavigateTo(entity); } } else { try { if (pos.Position.IsEmpty) FileService.OpenFile(pos.FileName); else FileService.JumpToFilePosition(pos.FileName, pos.Line, pos.Column); } catch (Exception ex) { MessageService.ShowException(ex, "Error jumping to '" + pos.FileName + "'."); } } }
public IClass GetClass(ResolveResult symbol) { if (symbol == null || !(symbol is TypeResolveResult)) { return null; } return ((TypeResolveResult)symbol).ResolvedClass; }
public IMember GetMember(ResolveResult symbol) { if (symbol == null || !(symbol is MemberResolveResult)) { return null; } return ((MemberResolveResult)symbol).ResolvedMember; }
public ResolveResult Resolve(string expression) { ExpressionResult expressionResult = new ExpressionResult(expression); PythonResolverContext context = new PythonResolverContext(ParseInfo, expressionResult, String.Empty); ResolveResult = Resolver.Resolve(context); return ResolveResult; }
public EventHandlerCompletitionDataProvider(string expression, ResolveResult resolveResult) { this.expression = expression; this.resolveResult = resolveResult; this.resolvedReturnType = resolveResult.ResolvedType; this.resolvedClass = resolvedReturnType.GetUnderlyingClass(); }
public Reference(string fileName, int offset, int length, string expression, ResolveResult resolveResult) { this.fileName = fileName; this.offset = offset; this.length = length; this.expression = expression; this.resolveResult = resolveResult; }
void ResolvePropertyExpression(string expression) { PythonResolverContext context = new PythonResolverContext(parseInfo); ExpressionResult expressionResult = new ExpressionResult(expression); PythonResolver resolver = new PythonResolver(); result = resolver.Resolve(context, expressionResult); }
protected override void RunImpl(ITextEditor editor, int offset, ResolveResult symbol) { var classUnderCaret = GetClass(symbol); if (classUnderCaret != null) { ContextActionsHelper.MakePopupWithBaseClasses(classUnderCaret).OpenAtCaretAndFocus(); return; } MessageService.ShowError("${res:ICSharpCode.Refactoring.NoClassUnderCursorError}"); }
IClass GetClass(ResolveResult currentSymbol) { if (currentSymbol == null || currentSymbol.ResolvedType == null) return null; IClass c = currentSymbol.ResolvedType.GetUnderlyingClass(); if (c == null) return null; c = c.ProjectContent.GetClass(c.FullyQualifiedName, c.TypeParameters.Count, c.ProjectContent.Language, GetClassOptions.LookForInnerClass); return ClassBookmarkSubmenuBuilder.GetCurrentPart(c); }
public RefactoringMenuContext(ITextEditor editor, ExpressionResult expressionResult, ResolveResult resolveResult, bool isDefinition, IProjectContent projectContent, ICompilationUnit compilationUnit) { this.Editor = editor; this.ExpressionResult = expressionResult; this.ResolveResult = resolveResult; this.IsDefinition = isDefinition; this.ProjectContent = projectContent; this.CompilationUnit = compilationUnit; }
public LocalResolveResult GetParameterAtCaret(ResolveResult symbol) { LocalResolveResult param = symbol as LocalResolveResult; if (param == null || param.CallingClass == null || param.ResolvedType == null) return null; if (param.CallingClass.ProjectContent.Language != LanguageProperties.CSharp) return null; if (!param.IsParameter) return null; return param; }
void MakeResult(IMember member) { IField field = member as IField; if (field != null && (field.IsLocalVariable || field.IsParameter)) { resolveResult = new LocalResolveResult(resolver.CallingMember, field); } else if (member != null) { resolveResult = new MemberResolveResult(callingClass, resolver.CallingMember, member); } else { ClearResult(); } }
public override IInsightItem[] ProvideInsight(ExpressionResult expressionResult, ResolveResult result) { if (result == null) return null; IReturnType type = result.ResolvedType; if (type == null) return null; return (from p in type.GetProperties() where p.IsIndexer select new MethodInsightItem(p) ).ToArray(); }
/// <summary> /// Tries to find a resource reference in the specified expression. /// </summary> /// <param name="expressionResult">The ExpressionResult for the expression.</param> /// <param name="expr">The AST representation of the full expression.</param> /// <param name="resolveResult">SharpDevelop's ResolveResult for the expression.</param> /// <param name="caretLine">The 0-based line where the expression is located.</param> /// <param name="caretColumn">The 0-based column where the expression is located.</param> /// <param name="fileName">The name of the source file where the expression is located.</param> /// <param name="fileContent">The content of the source file where the expression is located.</param> /// <param name="expressionFinder">The ExpressionFinder for the file.</param> /// <param name="charTyped">The character that has been typed at the caret position but is not yet in the buffer (this is used when invoked from code completion), or <c>null</c>.</param> /// <returns>A ResourceResolveResult describing the referenced resource, or <c>null</c>, if this expression does not reference a resource using the ICSharpCode.Core.ResourceService class.</returns> public ResourceResolveResult Resolve(ExpressionResult expressionResult, Expression expr, ResolveResult resolveResult, int caretLine, int caretColumn, string fileName, string fileContent, IExpressionFinder expressionFinder, char? charTyped) { IMember member = null; // "ResourceService.GetString(..." may be a MemberResolveResult or // MethodResolveResult, dependent on how much of the expression // has already been typed. MemberResolveResult mrr = resolveResult as MemberResolveResult; if (mrr != null) { // If it is a MemberResolveResult, this indicates that // the complete expression is already in the buffer. // So we only assign the member if Resolve is not invoked // from code completion to prevent the code completion window // from opening when typing something like: // ResourceService.GetString(...)[ if (charTyped == null) { member = mrr.ResolvedMember; } } else { MethodGroupResolveResult methrr = resolveResult as MethodGroupResolveResult; if (methrr != null) { // If it is a MethodResolveResult, the expression is incomplete. // Accept only if '(' has been typed. if (charTyped == '(') { member = methrr.GetMethodIfSingleOverload(); } } } if (member is IMethod && LanguageProperties.CSharp.NameComparer.Equals(member.FullyQualifiedName, "ICSharpCode.Core.ResourceService.GetString") ) { #if DEBUG LoggingService.Debug("ResourceToolkit: ICSharpCodeCoreNRefactoryResourceResolver: ResourceService resource access detected"); #endif string key = GetKeyFromExpression(expr); // TODO: Add information about return type (of the resource, if present). return new ResourceResolveResult(resolveResult.CallingClass, resolveResult.CallingMember, null, ICSharpCodeCoreResourceResolver.ResolveICSharpCodeCoreResourceSet(key, fileName), key); } return null; }
public void InitBase() { projectContent = new MockProjectContent(); PythonParser parser = new PythonParser(); string fileName = @"C:\projects\test\test.py"; compilationUnit = parser.Parse(projectContent, fileName, GetPythonScript()); parseInfo = new ParseInformation(compilationUnit); resolver = new PythonResolver(); expressionResult = GetExpressionResult(); resolveResult = resolver.Resolve(expressionResult, parseInfo, GetPythonScript()); }
/// <summary> /// In the code editor, highlights all references to the expression under the caret (for better code readability). /// </summary> public CaretReferencesRenderer(CodeEditorView editorView) { this.editorView = editorView; this.highlightRenderer = new ExpressionHighlightRenderer(this.editorView.TextArea.TextView); this.delayTimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(delayMs) }; this.delayTimer.Stop(); this.delayTimer.Tick += TimerTick; this.delayMoveTimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(delayMoveMs) }; this.delayMoveTimer.Stop(); this.delayMoveTimer.Tick += TimerMoveTick; this.editorView.TextArea.Caret.PositionChanged += CaretPositionChanged; // fixes SD-1873 - Unhandled WPF Exception when deleting text in text editor // clear highlights to avoid exceptions when trying to draw highlights in // locations that have been deleted already. this.editorView.Document.Changed += delegate { lastResolveResult = null; ClearHighlight(); }; }
protected override void RunImpl(ITextEditor editor, int offset, ResolveResult symbol) { if (symbol == null) return; FilePosition pos = symbol.GetDefinitionPosition(); if (pos.IsEmpty) return; try { if (pos.Position.IsEmpty) FileService.OpenFile(pos.FileName); else FileService.JumpToFilePosition(pos.FileName, pos.Line, pos.Column); } catch (Exception ex) { MessageService.ShowException(ex, "Error jumping to '" + pos.FileName + "'."); } }
protected override void RunImpl(ITextEditor editor, int offset, ResolveResult symbol) { var classUnderCaret = GetClass(symbol); if (classUnderCaret != null) { FindReferencesAndRenameHelper.RunFindReferences(classUnderCaret); return; } var memberUnderCaret = GetMember(symbol); if (memberUnderCaret != null) { FindReferencesAndRenameHelper.RunFindReferences(memberUnderCaret); return; } if (symbol is LocalResolveResult) { FindReferencesAndRenameHelper.RunFindReferences((LocalResolveResult)symbol); } }
Variable ResolveVariable(Variable variable) { Dom.ParseInformation info = ParserService.GetParseInformation(this.textEditor.FileName); Dom.ExpressionResult res = new Dom.ExpressionResult(variable.Name, Dom.DomRegion.FromLocation(variable.StartPos, variable.EndPos), Dom.ExpressionContext.Default, null); Dom.ResolveResult result = this.GetResolver().Resolve(res, info, this.textEditor.Document.TextContent); if (variable.Type.Type == "var") { variable.Type = Dom.Refactoring.CodeGenerator.ConvertType(result.ResolvedType, new Dom.ClassFinder(result.CallingMember)); } variable.IsReferenceType = result.ResolvedType.IsReferenceType == true; return(variable); }
public void SetUpFixture() { resolver = new PythonResolver(); mockProjectContent = new ICSharpCode.Scripting.Tests.Utils.MockProjectContent(); systemConsoleClass = new MockClass(mockProjectContent, "System.Console"); mockProjectContent.ClassToReturnFromGetClass = systemConsoleClass; compilationUnit = CreateCompilationUnit(mockProjectContent); ParseInformation parseInfo = new ParseInformation(compilationUnit); string python = GetPythonScript(); ExpressionResult expressionResult = new ExpressionResult("System.Console", new DomRegion(3, 2), null, null); resolveResult = resolver.Resolve(expressionResult, parseInfo, python); // Check that the best compilation unit is used and the resolve // still works. invalidMostRecentCompilationUnitResolveResult = resolver.Resolve(expressionResult, parseInfo, python); }
static string GetText(ResolveResult result) { if (result == null) { return null; } if (result is MixedResolveResult) return GetText(((MixedResolveResult)result).PrimaryResult); IAmbience ambience = MainForm.IsVisualBasic ? (IAmbience)new VBNetAmbience() : new CSharpAmbience(); ambience.ConversionFlags = ConversionFlags.StandardConversionFlags | ConversionFlags.ShowAccessibility; if (result is MemberResolveResult) { return GetMemberText(ambience, ((MemberResolveResult)result).ResolvedMember); } else if (result is LocalResolveResult) { LocalResolveResult rr = (LocalResolveResult)result; ambience.ConversionFlags = ConversionFlags.UseFullyQualifiedTypeNames | ConversionFlags.ShowReturnType; StringBuilder b = new StringBuilder(); if (rr.IsParameter) b.Append("parameter "); else b.Append("local variable "); b.Append(ambience.Convert(rr.Field)); return b.ToString(); } else if (result is NamespaceResolveResult) { return "namespace " + ((NamespaceResolveResult)result).Name; } else if (result is TypeResolveResult) { IClass c = ((TypeResolveResult)result).ResolvedClass; if (c != null) return GetMemberText(ambience, c); else return ambience.Convert(result.ResolvedType); } else if (result is MethodGroupResolveResult) { MethodGroupResolveResult mrr = result as MethodGroupResolveResult; IMethod m = mrr.GetMethodIfSingleOverload(); if (m != null) return GetMemberText(ambience, m); else return "Overload of " + ambience.Convert(mrr.ContainingType) + "." + mrr.Name; } else { return null; } }
/// <summary> /// Find all references to the resolved entity. /// </summary> public static List<Reference> FindReferences(ResolveResult entity, IProgressMonitor progressMonitor) { return FindReferences(entity, null, progressMonitor); }
static List<Reference> FindReferences(ResolveResult entity, string fileName, IProgressMonitor progressMonitor) { if (entity == null) throw new ArgumentNullException("entity"); if (entity is LocalResolveResult) { return RunFindReferences(entity.CallingClass, (entity as LocalResolveResult).Field, entity.CallingClass.CompilationUnit.FileName, progressMonitor); } else if (entity is TypeResolveResult) { TypeResolveResult trr = (TypeResolveResult)entity; if (trr.ResolvedClass != null) { return FindReferences(trr.ResolvedClass, fileName, progressMonitor); } } else if (entity is MemberResolveResult) { return FindReferences((entity as MemberResolveResult).ResolvedMember, fileName, progressMonitor); } else if (entity is MethodGroupResolveResult) { IMethod method = (entity as MethodGroupResolveResult).GetMethodIfSingleOverload(); if (method != null) { return FindReferences(method, fileName, progressMonitor); } } else if (entity is MixedResolveResult) { return FindReferences((entity as MixedResolveResult).PrimaryResult, fileName, progressMonitor); } return null; }
protected abstract void RunImpl(ITextEditor editor, int caretOffset, ResolveResult symbol);
/// <summary> /// Finds all references to the resolved entity, only in the file where the entity was resolved. /// </summary> public static List<Reference> FindReferencesLocal(ResolveResult entity, string fileName, IProgressMonitor progressMonitor) { return FindReferences(entity, fileName, progressMonitor); }
/// <summary> /// Tries to find a resource reference in the specified expression. /// </summary> /// <param name="expressionResult">The ExpressionResult for the expression.</param> /// <param name="expr">The AST representation of the full expression.</param> /// <param name="resolveResult">SharpDevelop's ResolveResult for the expression.</param> /// <param name="caretLine">The 0-based line where the expression is located.</param> /// <param name="caretColumn">The 0-based column where the expression is located.</param> /// <param name="fileName">The name of the source file where the expression is located.</param> /// <param name="fileContent">The content of the source file where the expression is located.</param> /// <param name="expressionFinder">The ExpressionFinder for the file.</param> /// <param name="charTyped">The character that has been typed at the caret position but is not yet in the buffer (this is used when invoked from code completion), or <c>null</c>.</param> /// <returns>A ResourceResolveResult describing the referenced resource, or <c>null</c>, if this expression does not reference a resource using the standard .NET framework classes.</returns> public ResourceResolveResult Resolve(ExpressionResult expressionResult, Expression expr, ResolveResult resolveResult, int caretLine, int caretColumn, string fileName, string fileContent, IExpressionFinder expressionFinder, char? charTyped) { /* * We need to catch the following cases here: * * Something.GetString( * Something.GetString("...") * Something.ApplyResources(obj, "...") * Something[ * Something["..."] * */ if (charTyped == '(') { // Something.GetString // This is a MethodResolveResult and we need the reference to "Something", // which is the next outer expression. // This is only valid when invoked from code completion // and the method invocation character ('(' in C# and VB) // has been typed. // This code is also reused when reducing a complete InvocationExpression // (MemberResolveResult) to the method reference by passing '(' as // charTyped explicitly. MethodGroupResolveResult methrr = resolveResult as MethodGroupResolveResult; if (methrr != null) { if ((methrr.Name == "GetString" || methrr.Name == "GetObject" || methrr.Name == "GetStream" || methrr.Name == "ApplyResources") && (resolveResult = NRefactoryAstCacheService.ResolveNextOuterExpression(ref expressionResult, caretLine, caretColumn, fileName, fileContent, expressionFinder)) != null) { return ResolveResource(resolveResult, expr); } else { return null; } } } // Do not use "else if" here. // '(' is also the IndexerExpressionStartToken for VB, // so the "else" block further down might still apply. if (charTyped == null) { // A MemberResolveResult with a complete expression // must only be considered a valid resource reference // when Resolve is not invoked from code completion // (i.e. charTyped == null) because this indicates // that the resource reference is already before the typed character // and we are not interested in the following expression. // This may happen when typing something like: // Something.GetString("...")[ MemberResolveResult mrr = resolveResult as MemberResolveResult; if (mrr != null) { if (mrr.ResolvedMember is IMethod && (mrr.ResolvedMember.Name == "GetString" || mrr.ResolvedMember.Name == "GetObject" || mrr.ResolvedMember.Name == "GetStream" || mrr.ResolvedMember.Name == "ApplyResources")) { // Something.GetString("...") // This is a MemberResolveResult and we need the reference to "Something". // The expression finder may only remove the string literal, so // we have to call Resolve again in this case to resolve // the method reference. if ((resolveResult = NRefactoryAstCacheService.ResolveNextOuterExpression(ref expressionResult, caretLine, caretColumn, fileName, fileContent, expressionFinder)) != null) { if (resolveResult is MethodGroupResolveResult) { return this.Resolve(expressionResult, expr, resolveResult, caretLine, caretColumn, fileName, fileContent, expressionFinder, '('); } else { return ResolveResource(resolveResult, expr); } } else { return null; } } else if (expr is IndexerExpression && IsResourceManager(mrr.ResolvedMember.DeclaringType.DefaultReturnType, fileName)) { // Something["..."] is an IndexerExpression. // We need the reference to Something and this is // the next outer expression. if ((resolveResult = NRefactoryAstCacheService.ResolveNextOuterExpression(ref expressionResult, caretLine, caretColumn, fileName, fileContent, expressionFinder)) != null) { return ResolveResource(resolveResult, expr); } else { return null; } } } } else { // This request is triggered from code completion. // The only case that has not been caught above is: // Something[ // The reference to "Something" is already in this expression. // So we have to test the trigger character against the // indexer expression start token of the file's language. LanguageProperties lp = NRefactoryResourceResolver.GetLanguagePropertiesForFile(fileName); if (lp != null && !String.IsNullOrEmpty(lp.IndexerExpressionStartToken) && lp.IndexerExpressionStartToken[0] == charTyped) { #if DEBUG LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: Indexer expression start typed, ResolveResult: "+resolveResult.ToString()); LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: -> Expression: "+expr.ToString()); #endif return ResolveResource(resolveResult, expr); } } return null; }
public static IEnumerable<AddUsingAction> GetAddUsingActions(ResolveResult symbol, ITextEditor editor) { if (symbol is UnknownIdentifierResolveResult) { return GetAddUsingActions((UnknownIdentifierResolveResult)symbol, editor); } else if (symbol is UnknownConstructorCallResolveResult) { return GetAddUsingActions((UnknownConstructorCallResolveResult)symbol, editor); } return new AddUsingAction[0]; }
public MixedResolveResult(ResolveResult primaryResult, ResolveResult secondaryResult) : base(primaryResult.CallingClass, primaryResult.CallingMember, primaryResult.ResolvedType) { this.primaryResult = primaryResult; this.secondaryResult = secondaryResult; }
/// <summary> /// Tries to find a resource reference in the specified expression. /// </summary> /// <param name="resolveResult">The ResolveResult that describes the referenced member.</param> /// <param name="expr">The AST representation of the full expression.</param> /// <returns> /// The ResourceResolveResult describing the referenced resource, if successful, /// or a null reference, if the referenced member is not a resource manager /// or if the resource file cannot be determined. /// </returns> static ResourceResolveResult ResolveResource(ResolveResult resolveResult, Expression expr) { ResourceSetReference rsr = null; MemberResolveResult mrr = resolveResult as MemberResolveResult; if (mrr != null) { rsr = ResolveResourceSet(mrr.ResolvedMember); } else { LocalResolveResult lrr = resolveResult as LocalResolveResult; if (lrr != null) { if (!lrr.IsParameter) { rsr = ResolveResourceSet(lrr.Field); } } } if (rsr != null) { bool isPrefixOnly; string key = GetKeyFromExpression(expr, out isPrefixOnly); if (isPrefixOnly) { return new ResourcePrefixResolveResult(resolveResult.CallingClass, resolveResult.CallingMember, null, rsr, key); } else { return new ResourceResolveResult(resolveResult.CallingClass, resolveResult.CallingMember, null, rsr, key); } } return null; }
public DelegateCallResolveResult(ResolveResult targetRR, IMethod delegateInvokeMethod) : base(targetRR.CallingClass, targetRR.CallingMember, delegateInvokeMethod.ReturnType) { this.targetRR = targetRR; this.delegateInvokeMethod = delegateInvokeMethod; }