protected override void OnExecute(Microsoft.VisualStudio.Shell.OleMenuCommand command) { int curLineIndex, curColumnIndex; VSTextView textView = new VSTextView(VSTextView.ActiveTextView); if (textView == null) { MessageBox.Show("textView is null"); return; } textView.GetCursorPositon(out curLineIndex, out curColumnIndex); //帮助:http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/c62a70eb-d84c-4410-890d-b41a86b2b55f/ ILangService langService; LangService_GetInstance(out langService); if (langService == null) { MessageBox.Show("langService is null"); return; } IDECompilerHost compilerHost = new IDECompilerHost(); IProject prj; Project currentPro = VSDocument.ActiveDocument.ProjectItem.ContainingProject; LangService_GetDteProject(langService, currentPro, out prj); if (prj == null) { MessageBox.Show("prj is null"); return; } FileName fileName = new Microsoft.RestrictedUsage.CSharp.Core.FileName(VSDocument.ActiveDocument.FullName); CSRPOSDATA data = new CSRPOSDATA() { LineIndex = curLineIndex, ColumnIndex = curColumnIndex }; CSharpMember m = Utilities.GetMemberAtLocation(compilerHost, prj, fileName.Value, data); if (m != null && m.ReturnType != null) { string returnName = m.ReturnType.GetFormattedName(InteropTypeFormatFlags.TypeFormatFlags_BestNameGUI); returnName = FilterReturnName(returnName); if (string.IsNullOrEmpty(returnName)) { return; } int insertCol = GetInsertPosition(textView, curLineIndex, curColumnIndex); textView.InsertText(returnName + " v =", curLineIndex, insertCol); textView.SelectText(curLineIndex, insertCol + returnName.Length + 1, curLineIndex, insertCol + returnName.Length + 2); } }
private static CSharpMember ResolveMember(CSharpMemberIdentifier memberIdentifier) { IDECompilerHost host = new IDECompilerHost(); var currentCompilations = host.Compilers.Select(i => i.GetCompilation()).ToList(); var resolved = ResolveMemberIdentifier(currentCompilations, memberIdentifier); if (resolved != null) { return(resolved.Item1); } return(null); }
void SolutionOpened() { Logger.PublicEntry(() => { try { this._compilerHost = new IDECompilerHost(); } catch { this._compilerHost = null; } _solutionLoaded = true; //FilesNeedingNewSemanticInfo.Clear(); //FilesNeedingNewSyntacticInfo.Clear(); }, "SolutionOpened"); }
static public bool IsModelReady(IDECompilerHost compilerHost) { Contract.Requires(compilerHost != null); try { if (compilerHost == null || compilerHost.Compilers == null) { return(false); } foreach (var compiler in compilerHost.Compilers) { var comp = compiler.GetCompilation(); foreach (var sourceFile in compiler.SourceFiles.Values) { var parseTree = sourceFile.GetParseTree(); var rootNode = parseTree.RootNode; } } } catch (InvalidOperationException e) { if (e.Message.Contains(InvalidOperationExceptionMessage_TheSnapshotIsOutOfDate)) { return(false); } else { throw e; } } catch (COMException e) { if (e.Message.Contains(COMExceptionMessage_BindingFailed)) { return(false); } else { throw e; } } return(true); }
/// <inheritdoc/> protected override void ReParseImpl() { Stopwatch stopwatch = Stopwatch.StartNew(); ITextSnapshot snapshot = TextBuffer.CurrentSnapshot; try { ITextDocument textDocument = TextDocument; string fileName = textDocument != null ? textDocument.FilePath : null; IDECompilerHost host = new IDECompilerHost(); IProject project = null; ILangService languageService; LangService_GetInstance(out languageService); if (languageService != null) { ICSharpTextBuffer csharpBuffer = languageService.FindTextBuffer(fileName); if (csharpBuffer != null) { project = csharpBuffer.Project; } } List <ITagSpan <IInheritanceTag> > tags = new List <ITagSpan <IInheritanceTag> >(); if (host != null && project != null && !string.IsNullOrEmpty(fileName)) { Compilation compilation = host.CreateCompiler(project).GetCompilation(); SourceFile sourceFile; if (!compilation.SourceFiles.TryGetValue(new FileName(fileName), out sourceFile)) { InheritanceParseResultEventArgs errorResult = new InheritanceParseResultEventArgs(snapshot, stopwatch.Elapsed, tags); OnParseComplete(errorResult); return; } ParseTree parseTree = sourceFile.GetParseTree(); SpecializedMatchingMemberCollector collector = new SpecializedMatchingMemberCollector(host.Compilers.Select(i => i.GetCompilation()), false); IEnumerable <ParseTreeNode> nodes = SelectTypes(parseTree); foreach (var node in nodes) { CSharpType type = null; type = compilation.GetTypeFromTypeDeclaration(node); if (type == null) { MarkDirty(true); return; } if (type.IsSealed) { continue; } // types which implement or derive from this type ISet <CSharpType> derivedClasses = collector.GetDerivedTypes(type.SymbolicIdentifier); if (derivedClasses.Count == 0) { continue; } StringBuilder builder = new StringBuilder(); string elementKindDisplayName = "types"; builder.AppendLine("Derived " + elementKindDisplayName + ":"); foreach (var derived in derivedClasses) { builder.AppendLine(" " + derived.GetFullTypeName()); } int nameIndex = node.Token; Token token = parseTree.LexData.Tokens[nameIndex]; ITextSnapshotLine line = snapshot.GetLineFromLineNumber(token.StartPosition.Line); SnapshotSpan span = new SnapshotSpan(snapshot, new Span(line.Start + token.StartPosition.Character, token.EndPosition.Character - token.StartPosition.Character)); InheritanceGlyph tag = type.IsInterface ? InheritanceGlyph.HasImplementations : InheritanceGlyph.Overridden; var targets = derivedClasses.Select(i => new TypeTarget(i.GetFullTypeName(), i.SymbolicIdentifier)); tags.Add(new TagSpan <IInheritanceTag>(span, _tagFactory.CreateTag(tag, builder.ToString().TrimEnd(), targets))); } nodes = parseTree.SelectMethodsPropertiesAndFields(); nodes = nodes.SelectMany(SelectDeclaratorsFromFields); foreach (var node in nodes) { if (node is AccessorDeclarationNode) { // these nodes always result in an ArgumentException in GetMemberFromMemberDeclaration continue; } CSharpMember member; try { member = compilation.GetMemberFromMemberDeclaration(node); } catch (ArgumentException) { continue; } if (member == null) { MarkDirty(true); return; } if (!SpecializedMatchingMemberCollector.IsSupportedMemberType(member)) { continue; } // methods which this method implements ISet <CSharpMemberIdentifier> implementedMethods = collector.GetImplementedInterfaceMembers(member.SymbolicIdentifier); // methods which this method overrides ISet <CSharpMemberIdentifier> overriddenMethods = collector.GetOverriddenBaseMembers(member.SymbolicIdentifier); // methods which override this method ISet <CSharpMemberIdentifier> overridingMethods = collector.GetOverridersFromDerivedTypes(member.SymbolicIdentifier); // methods which implement this method ISet <CSharpMemberIdentifier> implementingMethods = collector.GetImplementorsForInterfaceMember(member.SymbolicIdentifier); if (implementingMethods.Count == 0 && implementedMethods.Count == 0 && overriddenMethods.Count == 0 && overridingMethods.Count == 0) { continue; } StringBuilder builder = new StringBuilder(); string elementKindDisplayName = member.IsProperty ? "properties" : member.IsEvent ? "events" : "methods"; if (implementedMethods.Count > 0) { builder.AppendLine("Implemented " + elementKindDisplayName + ":"); foreach (var methodId in implementedMethods) { builder.AppendLine(" " + methodId.ToString()); } } if (overriddenMethods.Count > 0) { builder.AppendLine("Overridden " + elementKindDisplayName + ":"); foreach (var methodId in overriddenMethods) { builder.AppendLine(" " + methodId.ToString()); } } if (implementingMethods.Count > 0) { builder.AppendLine("Implementing " + elementKindDisplayName + " in derived types:"); foreach (var methodId in implementingMethods) { builder.AppendLine(" " + methodId.ToString()); } } if (overridingMethods.Count > 0) { builder.AppendLine("Overriding " + elementKindDisplayName + " in derived types:"); foreach (var methodId in overridingMethods) { builder.AppendLine(" " + methodId.ToString()); } } int nameIndex = node.Token; Token token = parseTree.LexData.Tokens[nameIndex]; ITextSnapshotLine line = snapshot.GetLineFromLineNumber(token.StartPosition.Line); SnapshotSpan span = new SnapshotSpan(snapshot, new Span(line.Start + token.StartPosition.Character, token.EndPosition.Character - token.StartPosition.Character)); InheritanceGlyph tag; if (implementedMethods.Count > 0) { if (overridingMethods.Count > 0) { tag = InheritanceGlyph.ImplementsAndOverridden; } else if (implementingMethods.Count > 0) { tag = InheritanceGlyph.ImplementsAndHasImplementations; } else { tag = InheritanceGlyph.Implements; } } else if (implementingMethods.Count > 0) { tag = InheritanceGlyph.HasImplementations; } else if (overriddenMethods.Count > 0) { if (overridingMethods.Count > 0) { tag = InheritanceGlyph.OverridesAndOverridden; } else { tag = InheritanceGlyph.Overrides; } } else { tag = InheritanceGlyph.Overridden; } List <CSharpMemberIdentifier> members = new List <CSharpMemberIdentifier>(); members.AddRange(implementedMethods); members.AddRange(overriddenMethods); members.AddRange(implementingMethods); members.AddRange(overridingMethods); var targets = members.Select(i => new MemberTarget(i)); tags.Add(new TagSpan <IInheritanceTag>(span, _tagFactory.CreateTag(tag, builder.ToString().TrimEnd(), targets))); } } InheritanceParseResultEventArgs result = new InheritanceParseResultEventArgs(snapshot, stopwatch.Elapsed, tags); OnParseComplete(result); } catch (InvalidOperationException) { MarkDirty(true); throw; } }
public CompilerHostShim(IDECompilerHost compilerHost) { _compilerHost = compilerHost; }