public static XSourceEntity FindElementInFile(XFile file, XPETypeSymbol petype, XSymbol element) { var walker = new SourceWalker(file, false); walker.Parse(false); var entities = walker.EntityList; XSourceEntity result = null; if (petype.IsFunctionsClass) { foreach (var entity in entities) { if (entity.Prototype == element.Prototype) { result = entity; break; } } } else { foreach (var entity in entities) { if (entity.FullName == element.FullName) { result = entity; break; } } } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="XSharpClassifier"/> class. /// </summary> /// <param name="registry">Classification registry.</param> internal XSharpClassifier(ITextBuffer buffer, IClassificationTypeRegistryService registry, ITextDocumentFactoryService factory) { XFile file = null; this._buffer = buffer; if (buffer.Properties.ContainsProperty(typeof(XFile))) { file = buffer.GetFile(); } if (file == null) { return; } _file = file; // xtraKeywords = new List <string>(); // Initialize our background workers _buffer.Changed += Buffer_Changed; _bwClassify = new BackgroundWorker(); _bwClassify.RunWorkerCompleted += ClassifyCompleted; _bwClassify.DoWork += DoClassify; _bwBuildModel = new BackgroundWorker(); _bwBuildModel.DoWork += BuildModelDoWork; _txtdocfactory = factory; if (xsharpKeywordType == null) { // These fields are static so only initialize the first time xsharpKeywordType = registry.GetClassificationType("keyword"); xsharpIdentifierType = registry.GetClassificationType("identifier"); xsharpCommentType = registry.GetClassificationType("comment"); xsharpOperatorType = registry.GetClassificationType("operator"); xsharpPunctuationType = registry.GetClassificationType("punctuation"); xsharpPPType = registry.GetClassificationType("preprocessor keyword"); xsharpNumberType = registry.GetClassificationType("number"); xsharpStringType = registry.GetClassificationType("string"); xsharpInactiveType = registry.GetClassificationType("excluded code"); xsharpBraceOpenType = registry.GetClassificationType(ColorizerConstants.XSharpBraceOpenFormat); xsharpBraceCloseType = registry.GetClassificationType(ColorizerConstants.XSharpBraceCloseFormat); xsharpLiteralType = registry.GetClassificationType("literal"); xsharpTextType = registry.GetClassificationType(ColorizerConstants.XSharpTextEndTextFormat); xsharpRegionStart = registry.GetClassificationType(ColorizerConstants.XSharpRegionStartFormat); xsharpRegionStop = registry.GetClassificationType(ColorizerConstants.XSharpRegionStopFormat); xsharpKwOpenType = registry.GetClassificationType(ColorizerConstants.XSharpBraceOpenFormat); xsharpKwCloseType = registry.GetClassificationType(ColorizerConstants.XSharpBraceCloseFormat); } // Run a synchronous scan to set the initial buffer colors _snapshot = buffer.CurrentSnapshot; _sourceWalker = new SourceWalker(file); ClassifyBuffer(_snapshot); _first = false; // start the model builder to do build a code model and the regions asynchronously try { _bwBuildModel.RunWorkerAsync(); } catch { } }
/// <summary> /// Initializes a new instance of the <see cref="XSharpClassifier"/> class. /// </summary> /// <param name="registry">Classification registry.</param> internal XSharpClassifier(ITextBuffer buffer, IClassificationTypeRegistryService registry, ITextDocumentFactoryService _) { XFile file = null; this._buffer = buffer; if (buffer.Properties.ContainsProperty(typeof(XFile))) { file = buffer.GetFile(); } if (file == null) { return; } // lineState = new XSharpLineState(buffer.CurrentSnapshot); xtraKeywords = new List <string>(); // Initialize our background workers _buffer.Changed += Buffer_Changed; if (xsharpKeywordType == null) { // These fields are static so only initialize the first time xsharpKeywordType = registry.GetClassificationType(PredefinedClassificationTypeNames.Keyword); xsharpIdentifierType = registry.GetClassificationType(PredefinedClassificationTypeNames.Identifier); xsharpCommentType = registry.GetClassificationType(PredefinedClassificationTypeNames.Comment); xsharpOperatorType = registry.GetClassificationType(PredefinedClassificationTypeNames.Operator); xsharpPPType = registry.GetClassificationType(PredefinedClassificationTypeNames.PreprocessorKeyword); xsharpNumberType = registry.GetClassificationType(PredefinedClassificationTypeNames.Number); xsharpStringType = registry.GetClassificationType(PredefinedClassificationTypeNames.String); xsharpInactiveType = registry.GetClassificationType(PredefinedClassificationTypeNames.ExcludedCode); xsharpLiteralType = registry.GetClassificationType(PredefinedClassificationTypeNames.Literal); xsharpTextType = registry.GetClassificationType(ColorizerConstants.XSharpTextEndTextFormat); xsharpRegionStart = registry.GetClassificationType(ColorizerConstants.XSharpRegionStartFormat); xsharpRegionStop = registry.GetClassificationType(ColorizerConstants.XSharpRegionStopFormat); xsharpKwOpenType = registry.GetClassificationType(ColorizerConstants.XSharpKwOpenFormat); xsharpKwCloseType = registry.GetClassificationType(ColorizerConstants.XSharpKwCloseFormat); } // Run a synchronous scan to set the initial buffer colors _sourceWalker = new SourceWalker(file); ClassifyBuffer(); _first = false; // start the model builder to do build a code model and the regions asynchronously LexAsync().FireAndForget(); }
/// <summary> /// Retrieve the locals for a particular member /// </summary> /// <param name="member"></param> /// <param name="snapshot"></param> /// <param name="iCurrentLine"></param> /// <returns></returns> internal static IList <XSourceVariableSymbol> GetLocals(this XSourceMemberSymbol member, XSharpSearchLocation location) { var iCurrentLine = Math.Min(location.Snapshot.LineCount - 1, location.LineNumber); // create a walker with just the contents of the current member // use a new file object so we will not destroy the types in the existing object var walker = new SourceWalker(new XFile(member.File.FullPath, member.File.Project), false); var start = member.Interval.Start; var end = member.Interval.Width; if (start + end > location.Snapshot.Length) { end = location.Snapshot.Length - start; } var memberSource = location.Snapshot.GetText(start, end); var locals = walker.ParseLocals(memberSource, member); // Add the normal locals for class members foreach (var local in locals) { // assign the current member so we will have the proper Parent as well local.Parent = member; local.File = member.File; } if (member.Kind.IsClassMember(location.Dialect) && !member.Modifiers.HasFlag(Modifiers.Static)) { var XVar = new XSourceVariableSymbol(member, "SELF", member.Range, member.Interval, member.ParentName); XVar.File = walker.File; locals.Add(XVar); if (!String.IsNullOrEmpty(member.ParentType.BaseTypeName)) { XVar = new XSourceVariableSymbol(member, "SUPER", member.Range, member.Interval, member.ParentType.BaseTypeName); XVar.File = walker.File; locals.Add(XVar); } } return(locals); }
public IEnumerable <ITagSpan <TextMarkerTag> > GetTags(NormalizedSnapshotSpanCollection spans) { DateTime oStart, oEnd; TimeSpan timeSpan; oStart = DateTime.Now; Debug("Start get brackets: " + oStart.ToString("hh:mm:ss.fff")); if (spans.Count == 0) //there is no content in the buffer { yield break; } if (CurrentChar == null || SourceBuffer == null) { yield break; } //don't do anything if the current SnapshotPoint is not initialized or at the end of the buffer if (!CurrentChar.HasValue || CurrentChar.Value.Position >= CurrentChar.Value.Snapshot.Length) { yield break; } //hold on to a snapshot of the current character SnapshotPoint ssp = CurrentChar.Value; //if the requested snapshot isn't the same as the one the brace is on, translate our spans to the expected snapshot if (spans[0].Snapshot != ssp.Snapshot) { ssp = ssp.TranslateTo(spans[0].Snapshot, PointTrackingMode.Positive); } //get the current char and the previous char char currentText = '\0'; char lastText = '\0'; SnapshotSpan pairSpan = new SnapshotSpan(); SnapshotPoint lastChar = new SnapshotPoint(); try { currentText = ssp.GetChar(); lastChar = ssp == 0 ? ssp : ssp - 1; //if ssp is 0 (beginning of buffer), don't move it back lastText = lastChar.GetChar(); } catch (Exception) { yield break; } // use the tokens stored in the buffer properties XSharpTokens xTokens = null; IList <IToken> tokens = null; int offset = 0; if (m_braceList.ContainsKey(currentText) || (m_braceList.ContainsValue(lastText))) //FM#081219 #1 - Only need to get the tokens if either of these conditions is true { if (SourceBuffer.Properties != null && SourceBuffer.Properties.ContainsProperty(typeof(XSharpTokens))) { xTokens = SourceBuffer.Properties.GetProperty <XSharpTokens>(typeof(XSharpTokens)); if (xTokens == null || xTokens.TokenStream == null || xTokens.SnapShot == null) { yield break; } tokens = xTokens.TokenStream.GetTokens(); if (tokens == null) { yield break; } if (xTokens.SnapShot.Version != ssp.Snapshot.Version) { // get source from the start of the file until the current entity var xfile = SourceBuffer.GetFile(); var member = XSharpTokenTools.FindMemberAtPosition(ssp.Position, xfile); if (member != null) { try { var sourceWalker = new SourceWalker(xfile); string text = ssp.Snapshot.GetText(); text = text.Substring(member.Interval.Start, member.Interval.Width); //FM#081219 #2 - We are in a 'member'. For brace matching we should only ever need to look to the end of this member offset = member.Interval.Start; Debug("Start sourceWalker.Lex: " + DateTime.Now.ToString("hh:mm:ss.fff")); var stream = (BufferedTokenStream)sourceWalker.Lex(text); Debug("End sourceWalker.Lex: " + DateTime.Now.ToString("hh:mm:ss.fff")); tokens = stream.GetTokens(); } catch (Exception e) { // if it crashes, that might be because the snapshot used for the Lex/Parse is no more // so, we may have a too much difference // we do not break but simply use the 'old' tokens System.Diagnostics.Debug.WriteLine(e.Message); } } } } } // First, try to match Simple chars if (m_braceList.ContainsKey(currentText)) //the key is the open brace { char closeChar; m_braceList.TryGetValue(currentText, out closeChar); if (BraceMatchingTagger.FindMatchingCloseChar(ssp, currentText, closeChar, out pairSpan, tokens, offset) == true) { yield return(new TagSpan <TextMarkerTag>(new SnapshotSpan(ssp, 1), new TextMarkerTag("blue"))); yield return(new TagSpan <TextMarkerTag>(pairSpan, new TextMarkerTag("blue"))); } } else if (m_braceList.ContainsValue(lastText)) //the value is the close brace, which is the *previous* character { var open = from n in m_braceList where n.Value.Equals(lastText) select n.Key; if (BraceMatchingTagger.FindMatchingOpenChar(lastChar, (char)open.ElementAt <char>(0), lastText, out pairSpan, tokens, offset) == true) { yield return(new TagSpan <TextMarkerTag>(new SnapshotSpan(lastChar, 1), new TextMarkerTag("blue"))); yield return(new TagSpan <TextMarkerTag>(pairSpan, new TextMarkerTag("blue"))); } } else { // Second, try to Match Keywords // Try to retrieve an already parsed list of Tags XSharpClassifier xsClassifier = null; if (SourceBuffer.Properties.ContainsProperty(typeof(XSharpClassifier))) { xsClassifier = SourceBuffer.Properties[typeof(XSharpClassifier)] as XSharpClassifier; } if (xsClassifier != null) { ITextSnapshot snapshot = xsClassifier.Snapshot; if (snapshot.Version != ssp.Snapshot.Version) { yield break; } SnapshotSpan Span = new SnapshotSpan(snapshot, 0, snapshot.Length); var classifications = xsClassifier.GetTags(); // We cannot use SortedList, because we may have several Classification that start at the same position List <ClassificationSpan> sortedTags = new List <ClassificationSpan>(); foreach (var tag in classifications) { // Only keep the Brace matching Tags if ((tag.ClassificationType.IsOfType(ColorizerConstants.XSharpBraceOpenFormat)) || (tag.ClassificationType.IsOfType(ColorizerConstants.XSharpBraceCloseFormat))) { sortedTags.Add(tag); } } sortedTags.Sort((a, b) => a.Span.Start.Position.CompareTo(b.Span.Start.Position) * 1000 + string.Compare(a.ClassificationType.Classification, b.ClassificationType.Classification)); // var tags = sortedTags.Where(x => ssp.Position >= x.Span.Start.Position && ssp.Position <= x.Span.End.Position); foreach (var currentTag in tags) { var index = sortedTags.IndexOf(currentTag); if (currentTag.ClassificationType.IsOfType(ColorizerConstants.XSharpBraceOpenFormat)) { if (FindMatchingCloseTag(sortedTags, index, snapshot, out pairSpan)) { var span = currentTag.Span; yield return(new TagSpan <TextMarkerTag>(span, new TextMarkerTag("bracehighlight"))); yield return(new TagSpan <TextMarkerTag>(pairSpan, new TextMarkerTag("bracehighlight"))); } } else { if (FindMatchingOpenTag(sortedTags, index, snapshot, out pairSpan)) { var span = currentTag.Span; yield return(new TagSpan <TextMarkerTag>(pairSpan, new TextMarkerTag("bracehighlight"))); yield return(new TagSpan <TextMarkerTag>(span, new TextMarkerTag("bracehighlight"))); } } } } } oEnd = DateTime.Now; timeSpan = oEnd - oStart; Debug("Finished get brackets: " + oEnd.ToString("hh:mm:ss.fff")); Debug("Finished get brackets - total ms: " + timeSpan.TotalMilliseconds.ToString()); }