private GotoImplementationResponse GetMemberResponse(CSharpTypeResolveContext rctx, MemberResolveResult resolveResult) { var locations = new List<Location>(); //TODO: we don't need to scan all types in all projects foreach (IUnresolvedTypeDefinition type in GetAllTypes()) { ITypeDefinition resolvedDef = type.Resolve(rctx).GetDefinition(); if (resolvedDef != null) { IMember member = InheritanceHelper.GetDerivedMember(resolveResult.Member, resolvedDef); if (member != null) { var region = member.MemberDefinition.Region; var location = new Location { FileName = region.FileName, Line = region.BeginLine, Column = region.BeginColumn }; locations.Add(location); } } } return new GotoImplementationResponse { Locations = locations }; }
IType ITypeReference.Resolve(ITypeResolveContext context) { // Strictly speaking, we might have to resolve the type in a nested compilation, similar // to what we're doing with ConstantExpression. // However, in almost all cases this will work correctly - if the resulting type is only available in the // nested compilation and not in this, we wouldn't be able to map it anyways. var ctx = context as CSharpTypeResolveContext; if (ctx == null) { ctx = new CSharpTypeResolveContext(context.CurrentAssembly ?? context.Compilation.MainAssembly, null, context.CurrentTypeDefinition, context.CurrentMember); } return ResolveType(new CSharpResolver(ctx)); // A potential issue might be this scenario: // Assembly 1: // class A { public class Nested {} } // Assembly 2: (references asm 1) // class B : A {} // Assembly 3: (references asm 1 and 2) // class C { public B.Nested Field; } // Assembly 4: (references asm 1 and 3, but not 2): // uses C.Field; // Here we would not be able to resolve 'B.Nested' in the compilation of assembly 4, as type B is missing there. }
public CSharpResolver(ICompilation compilation) { if (compilation == null) throw new ArgumentNullException("compilation"); this.compilation = compilation; this.conversions = Conversions.Get(compilation); this.context = new CSharpTypeResolveContext(compilation.MainAssembly); }
public CSharpResolvedAttribute(CSharpTypeResolveContext context, CSharpAttribute unresolved) { this.context = context; this.unresolved = unresolved; // Pretty much any access to the attribute checks the type first, so // we don't need to use lazy-loading for that. this.attributeType = unresolved.AttributeType.Resolve(context); }
public OverrideCompletionData(int declarationBegin, IMember m, CSharpTypeResolveContext contextAtCaret) : base(m) { this.declarationBegin = declarationBegin; this.contextAtCaret = contextAtCaret; var ambience = new CSharpAmbience(); ambience.ConversionFlags = ConversionFlags.ShowTypeParameterList | ConversionFlags.ShowParameterList | ConversionFlags.ShowParameterNames; this.CompletionText = ambience.ConvertEntity(m); }
public CSharpParameterCompletionEngine (IDocument document, IParameterCompletionDataFactory factory, IProjectContent content, CSharpTypeResolveContext ctx, CompilationUnit unit, CSharpParsedFile parsedFile) : base (content, ctx, unit, parsedFile) { if (document == null) throw new ArgumentNullException ("document"); if (factory == null) throw new ArgumentNullException ("factory"); this.document = document; this.factory = factory; }
public static string GetOverrideTargetName (IMember m, CSharpTypeResolveContext resolveContext) { var builder = new TypeSystemAstBuilder (new CSharpResolver(resolveContext)); return builder.ConvertEntity(m).ToString() // Builder automatically adds a trailing newline .TrimEnd(Environment.NewLine.ToCharArray()); }
public CSharpResolver(CSharpTypeResolveContext context) { if (context == null) throw new ArgumentNullException("context"); this.compilation = context.Compilation; this.conversions = Conversions.Get(compilation); this.context = context; if (context.CurrentTypeDefinition != null) currentTypeDefinitionCache = new TypeDefinitionCache(context.CurrentTypeDefinition); }
public CSharpParameterCompletionEngine(IDocument document, ICompletionContextProvider completionContextProvider, IParameterCompletionDataFactory factory, IProjectContent content, CSharpTypeResolveContext ctx) : base (content, completionContextProvider, ctx) { if (document == null) { throw new ArgumentNullException("document"); } if (factory == null) { throw new ArgumentNullException("factory"); } this.document = document; this.factory = factory; }
private CSharpResolver(ICompilation compilation, CSharpConversions conversions, CSharpTypeResolveContext context, bool checkForOverflow, bool isWithinLambdaExpression, TypeDefinitionCache currentTypeDefinitionCache, ImmutableStack<IVariable> localVariableStack, ObjectInitializerContext objectInitializerStack) { this.compilation = compilation; this.conversions = conversions; this.context = context; this.checkForOverflow = checkForOverflow; this.isWithinLambdaExpression = isWithinLambdaExpression; this.currentTypeDefinitionCache = currentTypeDefinitionCache; this.localVariableStack = localVariableStack; this.objectInitializerStack = objectInitializerStack; }
public GetOverrideTargetsResponse ( IMember m , CSharpTypeResolveContext resolveContext) { if (resolveContext == null) throw new ArgumentNullException("resolveContext"); if (m == null) throw new ArgumentNullException("m"); this.OverrideTargetName = GetOverrideTargetName(m, resolveContext); }
private QuickFixResponse GetTypeResponse(CSharpTypeResolveContext rctx, ITypeDefinition typeDefinition) { var types = GetAllTypes().Select(t => t.Resolve(rctx).GetDefinition()); var quickFixes = from type in types where type != null && type != typeDefinition && type.IsDerivedFrom(typeDefinition) select QuickFix.ForFirstLineInRegion ( type.Region , _solution.GetFile(type.Region.FileName)); return new QuickFixResponse(quickFixes); }
public CSharpResolver(ICompilation compilation) { if (compilation == null) throw new ArgumentNullException("compilation"); this.compilation = compilation; this.conversions = CSharpConversions.Get(compilation); this.context = new CSharpTypeResolveContext(compilation.MainAssembly); var pc = compilation.MainAssembly.UnresolvedAssembly as CSharpProjectContent; if (pc != null) { this.checkForOverflow = pc.CompilerSettings.CheckForOverflow; } }
private CSharpCompletionContext(ITextEditor editor, CSharpFullParseInformation parseInfo, ICompilation compilation, IProjectContent projectContent) { Debug.Assert(editor != null); Debug.Assert(parseInfo != null); Debug.Assert(compilation != null); Debug.Assert(projectContent != null); this.Editor = editor; this.ParseInformation = parseInfo; this.Compilation = compilation; this.ProjectContent = projectContent; this.TypeResolveContextAtCaret = parseInfo.UnresolvedFile.GetTypeResolveContext(compilation, editor.Caret.Location); this.CompletionContextProvider = new DefaultCompletionContextProvider(editor.Document, parseInfo.UnresolvedFile); }
public CSharpCodeCompletionContext(IDocument document, int offset, IProjectContent projectContent) { this.document = new ReadOnlyDocument(document, document.FileName); this.offset = offset; var unresolvedFile = CSharpParsingHelpers.CreateCSharpUnresolvedFile(this.document); this.projectContent = projectContent.AddOrUpdateFiles(unresolvedFile); completionContextProvider = new DefaultCompletionContextProvider(this.document, unresolvedFile); var compilation = this.projectContent.CreateCompilation(); var location = this.document.GetLocation(this.offset); typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); }
protected CSharpCompletionEngineBase(IProjectContent content, ICompletionContextProvider completionContextProvider, CSharpTypeResolveContext ctx) { if (content == null) throw new ArgumentNullException("content"); if (ctx == null) throw new ArgumentNullException("ctx"); if (completionContextProvider == null) throw new ArgumentNullException("completionContextProvider"); this.ProjectContent = content; this.CompletionContextProvider = completionContextProvider; this.ctx = ctx; }
public CSharpCompletionEngine (IDocument document, ICompletionDataFactory factory, IProjectContent content, CSharpTypeResolveContext ctx, CompilationUnit unit, CSharpParsedFile parsedFile) : base (content, ctx, unit, parsedFile) { if (document == null) throw new ArgumentNullException ("document"); if (factory == null) throw new ArgumentNullException ("factory"); this.document = document; this.factory = factory; // Set defaults for additional input properties this.FormattingPolicy = new CSharpFormattingOptions(); this.EolMarker = Environment.NewLine; this.IndentString = "\t"; }
public CSharpCompletionEngine(IDocument document, ICompletionContextProvider completionContextProvider, ICompletionDataFactory factory, IProjectContent content, CSharpTypeResolveContext ctx) : base (content, completionContextProvider, ctx) { if (document == null) { throw new ArgumentNullException("document"); } if (factory == null) { throw new ArgumentNullException("factory"); } this.document = document; this.factory = factory; // Set defaults for additional input properties this.FormattingPolicy = FormattingOptionsFactory.CreateMono(); this.EolMarker = Environment.NewLine; this.IndentString = "\t"; }
public ImportCompletionData(ITypeDefinition typeDef, CSharpTypeResolveContext contextAtCaret, bool useFullName) : base(typeDef) { this.Description = "using " + typeDef.Namespace + ";"; if (useFullName) { var astBuilder = new TypeSystemAstBuilder(new CSharpResolver(contextAtCaret)); insertionText = astBuilder.ConvertType(typeDef).GetText(); } else { insertionText = typeDef.Name; insertUsing = typeDef.Namespace; } }
public ResolvedUsingScope(CSharpTypeResolveContext context, UsingScope usingScope) { if (context == null) throw new ArgumentNullException("context"); if (usingScope == null) throw new ArgumentNullException("usingScope"); this.parentContext = context; this.usingScope = usingScope; if (usingScope.Parent != null) { if (context.CurrentUsingScope == null) throw new InvalidOperationException(); } else { if (context.CurrentUsingScope != null) throw new InvalidOperationException(); } }
protected CSharpCompletionEngineBase (IProjectContent content, CSharpTypeResolveContext ctx, CompilationUnit unit, CSharpParsedFile parsedFile) { if (content == null) throw new ArgumentNullException ("content"); if (ctx == null) throw new ArgumentNullException ("ctx"); if (unit == null) throw new ArgumentNullException ("unit"); if (parsedFile == null) throw new ArgumentNullException ("parsedFile"); this.ProjectContent = content; this.ctx = ctx; this.Unit = unit; this.CSharpParsedFile = parsedFile; }
IList<IAttribute> GetAttributes(ref IList<IAttribute> field, bool assemblyAttributes) { IList<IAttribute> result = LazyInit.VolatileRead(ref field); if (result != null) { return result; } else { result = new List<IAttribute>(); foreach (var parsedFile in projectContent.Files.OfType<CSharpParsedFile>()) { var attributes = assemblyAttributes ? parsedFile.AssemblyAttributes : parsedFile.ModuleAttributes; var context = new CSharpTypeResolveContext(this, parsedFile.RootUsingScope.Resolve(compilation)); foreach (var unresolvedAttr in attributes) { result.Add(unresolvedAttr.CreateResolvedAttribute(context)); } } return LazyInit.GetOrSet(ref field, result); } }
/// <summary> /// Initializes a new instance of the <see cref="CSharpCompletionContext"/> class. /// </summary> /// <param name="document">The document, make sure the FileName property is set on the document.</param> /// <param name="offset">The offset.</param> /// <param name="projectContent">Content of the project.</param> /// <param name="usings">The usings.</param> public CSharpCompletionContext(IDocument document, int offset, IProjectContent projectContent, string usings = null) { OriginalDocument = document; OriginalOffset = offset; //if the document is a c# script we have to soround the document with some code. Document = PrepareCompletionDocument(document, ref offset, usings); Offset = offset; var syntaxTree = new CSharpParser().Parse(Document, Document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); ProjectContent = projectContent.AddOrUpdateFiles(unresolvedFile); //note: it's important that the project content is used that is returned after adding the unresolved file Compilation = ProjectContent.CreateCompilation(); var location = Document.GetLocation(Offset); Resolver = unresolvedFile.GetResolver(Compilation, location); TypeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(Compilation, location); CompletionContextProvider = new DefaultCompletionContextProvider(Document, unresolvedFile); }
private QuickFixResponse GetMemberResponse(CSharpTypeResolveContext rctx, MemberResolveResult resolveResult) { var quickFixes = new List<QuickFix>(); //TODO: we don't need to scan all types in all projects foreach (IUnresolvedTypeDefinition type in GetAllTypes()) { ITypeDefinition resolvedDef = type.Resolve(rctx).GetDefinition(); if (resolvedDef != null) { IMember member = InheritanceHelper.GetDerivedMember(resolveResult.Member, resolvedDef); if (member != null) { var quickFix = QuickFix.ForFirstLineInRegion ( member.MemberDefinition.Region , _solution.GetFile(type.Region.FileName)); quickFixes.Add(quickFix); } } } return new QuickFixResponse(quickFixes); }
public QuickFixResponse FindDerivedMembersAsQuickFixes(GotoImplementationRequest request) { var res = _bufferParser.ParsedContent(request.Buffer, request.FileName); var loc = new TextLocation(request.Line, request.Column); ResolveResult resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc); var rctx = new CSharpTypeResolveContext(res.Compilation.MainAssembly); var usingScope = res.UnresolvedFile.GetUsingScope(loc).Resolve(res.Compilation); rctx = rctx.WithUsingScope(usingScope); if (resolveResult is TypeResolveResult) { return GetTypeResponse(rctx, resolveResult.Type.GetDefinition()); } if (resolveResult is MemberResolveResult) { return GetMemberResponse(rctx, resolveResult as MemberResolveResult); } return new QuickFixResponse(); }
static int GetIndex(string text) { var editorText = new StringBuilder(); int trigger = 0, end = 0; for (int i = 0; i < text.Length; i++) { if (text[i] == '@') { trigger = editorText.Length; continue; } if (text[i] == '$') { end = editorText.Length; continue; } editorText.Append(text [i]); } var doc = new ReadOnlyDocument(editorText.ToString ()); var pctx = new CSharpProjectContent(); var rctx = new CSharpTypeResolveContext(pctx.CreateCompilation().MainAssembly); var ctxProvider = new DefaultCompletionContextProvider(doc, new CSharpUnresolvedFile()); var engine = new CSharpParameterCompletionEngine(doc, ctxProvider, new ParameterCompletionTests.TestFactory(pctx), pctx, rctx); return engine.GetCurrentParameterIndex(trigger, end); }
public override CodeGeneratorMemberResult CreateMemberImplementation (ITypeDefinition implementingType, IUnresolvedTypeDefinition part, IUnresolvedMember member, bool explicitDeclaration) { SetIndentTo (part); var options = new CodeGenerationOptions () { ExplicitDeclaration = explicitDeclaration, ImplementingType = implementingType, Part = part }; ITypeResolveContext ctx; var doc = IdeApp.Workbench.GetDocument (part.Region.FileName); ctx = new CSharpTypeResolveContext (implementingType.Compilation.MainAssembly, null, implementingType, null); options.Document = doc; if (member is IUnresolvedMethod) return GenerateCode ((IMethod) ((IUnresolvedMethod)member).CreateResolved (ctx), options); if (member is IUnresolvedProperty) return GenerateCode ((IProperty) ((IUnresolvedProperty)member).CreateResolved (ctx), options); if (member is IUnresolvedField) return GenerateCode ((IField) ((IUnresolvedField)member).CreateResolved (ctx), options); if (member is IUnresolvedEvent) return GenerateCode ((IEvent) ((IUnresolvedEvent)member).CreateResolved (ctx), options); throw new NotSupportedException ("member " + member + " is not supported."); }
static CompletionDataList CreateProvider (string text, bool isCtrlSpace) { string parsedText; string editorText; int cursorPosition = text.IndexOf ('$'); int endPos = text.IndexOf ('$', cursorPosition + 1); if (endPos == -1) { parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1); } else { parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1); editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1); cursorPosition = endPos - 1; } var doc = new ReadOnlyDocument (editorText); IProjectContent pctx = new CSharpProjectContent (); pctx = pctx.AddAssemblyReferences (new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore }); var compilationUnit = new CSharpParser ().Parse (parsedText, "program.cs"); compilationUnit.Freeze (); var parsedFile = compilationUnit.ToTypeSystem (); pctx = pctx.UpdateProjectContent (null, parsedFile); var cmp = pctx.CreateCompilation (); var loc = doc.GetLocation (cursorPosition); var rctx = new CSharpTypeResolveContext (cmp.MainAssembly); rctx = rctx.WithUsingScope (parsedFile.GetUsingScope (loc).Resolve (cmp)); var curDef = parsedFile.GetInnermostTypeDefinition (loc); if (curDef != null) { var resolvedDef = curDef.Resolve (rctx).GetDefinition (); rctx = rctx.WithCurrentTypeDefinition (resolvedDef); var curMember = resolvedDef.Members.FirstOrDefault (m => m.Region.Begin <= loc && loc < m.BodyRegion.End); if (curMember != null) rctx = rctx.WithCurrentMember (curMember); } var engine = new CSharpCompletionEngine (doc, new TestFactory (), pctx, rctx, compilationUnit, parsedFile); engine.EolMarker = Environment.NewLine; engine.FormattingPolicy = FormattingOptionsFactory.CreateMono (); var data = engine.GetCompletionData (cursorPosition, isCtrlSpace); return new CompletionDataList () { Data = data, AutoCompleteEmptyMatch = engine.AutoCompleteEmptyMatch, AutoSelect = engine.AutoSelect, DefaultCompletionString = engine.DefaultCompletionString }; }
public CSharpCompletionDataFactory(CSharpTypeResolveContext contextAtCaret, CSharpCompletionContext context) { Debug.Assert(contextAtCaret != null); this.contextAtCaret = contextAtCaret; this.context = context; }
/// <summary> /// Resolves the namespace represented by this using scope. /// </summary> public ResolvedUsingScope Resolve(ICompilation compilation) { CacheManager cache = compilation.CacheManager; ResolvedUsingScope resolved = cache.GetShared(this) as ResolvedUsingScope; if (resolved == null) { var csContext = new CSharpTypeResolveContext(compilation.MainAssembly, parent != null ? parent.Resolve(compilation) : null); resolved = (ResolvedUsingScope)cache.GetOrAddShared(this, new ResolvedUsingScope(csContext, this)); } return resolved; }