public ParseInformation Parse(FileName fileName, ITextSource fileContent, bool fullParseInformationRequested, IProject parentProject, CancellationToken cancellationToken) { AXmlParser parser = new AXmlParser(); AXmlDocument document; IncrementalParserState newParserState; if (fileContent.Version is OnDiskTextSourceVersion) { document = parser.Parse(fileContent, cancellationToken); newParserState = null; } else { document = parser.ParseIncremental(parserState, fileContent, out newParserState, cancellationToken); } parserState = newParserState; XamlUnresolvedFile unresolvedFile = XamlUnresolvedFile.Create(fileName, fileContent, document); ParseInformation parseInfo; if (fullParseInformationRequested) { parseInfo = new XamlFullParseInformation(unresolvedFile, document, fileContent.CreateSnapshot()); } else { parseInfo = new ParseInformation(unresolvedFile, fileContent.Version, false); } AddTagComments(document, parseInfo, fileContent); return(parseInfo); }
public XamlAstResolver(ICompilation compilation, XamlFullParseInformation parseInfo) { if (compilation == null) throw new ArgumentNullException("compilation"); if (parseInfo == null) throw new ArgumentNullException("parseInfo"); this.compilation = compilation; this.parseInfo = parseInfo; this.textDocument = new ReadOnlyDocument(parseInfo.Text, parseInfo.FileName); }
public XamlAstResolver(ICompilation compilation, XamlFullParseInformation parseInfo) { if (compilation == null) { throw new ArgumentNullException("compilation"); } if (parseInfo == null) { throw new ArgumentNullException("parseInfo"); } this.compilation = compilation; this.parseInfo = parseInfo; this.textDocument = new ReadOnlyDocument(parseInfo.Text, parseInfo.FileName); }
public ParseInformation Parse(FileName fileName, ITextSource fileContent, bool fullParseInformationRequested, IProject parentProject, CancellationToken cancellationToken) { AXmlParser parser = new AXmlParser(); AXmlDocument document; IncrementalParserState newParserState; if (fileContent.Version is OnDiskTextSourceVersion) { document = parser.Parse(fileContent, cancellationToken); newParserState = null; } else { document = parser.ParseIncremental(parserState, fileContent, out newParserState, cancellationToken); } parserState = newParserState; XamlUnresolvedFile unresolvedFile = XamlUnresolvedFile.Create(fileName, fileContent, document); ParseInformation parseInfo; if (fullParseInformationRequested) parseInfo = new XamlFullParseInformation(unresolvedFile, document, fileContent.CreateSnapshot()); else parseInfo = new ParseInformation(unresolvedFile, fileContent.Version, false); AddTagComments(document, parseInfo, fileContent); return parseInfo; }
public XamlCompletionItemList CreateListForContext(XamlCompletionContext context) { XamlCompletionItemList list = new XamlCompletionItemList(context); ITextEditor editor = context.Editor; compilation = SD.ParserService.GetCompilationForFile(editor.FileName); XamlAstResolver resolver = new XamlAstResolver(compilation, context.ParseInformation); switch (context.Description) { case XamlContextDescription.None: if (context.Forced) { list.Items.AddRange(standardElements); list.Items.AddRange(CreateElementList(context, false)); AddClosingTagCompletion(context, list, resolver); } break; case XamlContextDescription.AtTag: if ((editor.Caret.Offset > 0 && editor.Document.GetCharAt(editor.Caret.Offset - 1) == '.') || context.PressedKey == '.') { list.Items.AddRange(CreateAttributeList(context, false)); } else { list.Items.AddRange(standardElements); list.Items.AddRange(CreateElementList(context, false)); AddClosingTagCompletion(context, list, resolver); } break; case XamlContextDescription.InTag: string word = editor.GetWordBeforeCaretExtended(); if (context.PressedKey == '.' || word.Contains(".")) { int pos = word.IndexOf(':'); string element = word.Substring(pos + 1, word.Length - pos - 1); string className = word; int propertyStart = element.IndexOf('.'); if (propertyStart != -1) { element = element.Substring(0, propertyStart).TrimEnd('.'); className = className.Substring(0, propertyStart + pos + 1).TrimEnd('.'); } int caretOffset = editor.Caret.Offset; int offset = editor.Document.LastIndexOf(className, caretOffset - word.Length, word.Length, StringComparison.OrdinalIgnoreCase); TextLocation loc = editor.Document.GetLocation(offset); XamlFullParseInformation info = context.ParseInformation; XamlResolver nameResolver = new XamlResolver(compilation); TypeResolveResult trr = nameResolver.ResolveExpression(className, context) as TypeResolveResult; ITypeDefinition typeClass = trr != null?trr.Type.GetDefinition() : null; if (typeClass != null && typeClass.HasAttached(true, true)) { list.Items.AddRange(GetListOfAttached(context, typeClass, true, true)); } } else { list.Items.AddRange(CreateAttributeList(context, true)); list.Items.AddRange(standardAttributes); } break; case XamlContextDescription.InAttributeValue: new XamlCodeCompletionBinding().CtrlSpace(editor); break; } list.SortItems(); return(list); }
public static XamlContext ResolveContext(FileName fileName, ITextSource fileContent, int offset) { XamlFullParseInformation info = SD.ParserService.Parse(fileName, fileContent) as XamlFullParseInformation; if (info == null) { throw new Exception("need full parse info!"); } AXmlDocument document = info.Document; AXmlObject currentData = document.GetChildAtOffset(offset); string attributeName = string.Empty, attributeValue = string.Empty; AttributeValue value = null; bool isRoot = false; bool wasAXmlElement = false; int offsetFromValueStart = -1; List <AXmlElement> ancestors = new List <AXmlElement>(); Dictionary <string, XamlNamespace> xmlns = new Dictionary <string, XamlNamespace>(); List <string> ignored = new List <string>(); string xamlNamespacePrefix = string.Empty; var item = currentData; AXmlElement root = null; while (item != document) { if (item is AXmlElement) { AXmlElement element = item as AXmlElement; ancestors.Add(element); foreach (var attr in element.Attributes) { if (attr.IsNamespaceDeclaration) { string prefix = (attr.Name == "xmlns") ? "" : attr.LocalName; if (!xmlns.ContainsKey(prefix)) { xmlns.Add(prefix, new XamlNamespace(attr.Value)); } } if (attr.LocalName == "Ignorable" && attr.Namespace == XamlConst.MarkupCompatibilityNamespace) { ignored.AddRange(attr.Value.Split(' ', '\t')); } if (string.IsNullOrEmpty(xamlNamespacePrefix) && attr.Value == XamlConst.XamlNamespace) { xamlNamespacePrefix = attr.LocalName; } } if (element.Parent is AXmlDocument) { root = element; if (!wasAXmlElement) { isRoot = true; } } wasAXmlElement = true; } item = item.Parent; } XamlContextDescription description = XamlContextDescription.None; AXmlElement active = null; AXmlElement parent = null; if (currentData is AXmlAttribute) { AXmlAttribute a = currentData as AXmlAttribute; int valueStartOffset = a.ValueSegment.Offset + 1; attributeName = a.Name; attributeValue = a.Value; value = MarkupExtensionParser.ParseValue(attributeValue); if (offset >= valueStartOffset && (offset < a.EndOffset || // if length is < 2 one quote is missing (a.ValueSegment.Length <= 1 && offset <= a.EndOffset))) { offsetFromValueStart = offset - valueStartOffset; description = XamlContextDescription.InAttributeValue; if (value != null && !value.IsString) { description = XamlContextDescription.InMarkupExtension; } if (attributeValue.StartsWith("{}", StringComparison.Ordinal) && attributeValue.Length > 2) { description = XamlContextDescription.InAttributeValue; } } else { description = XamlContextDescription.InTag; } active = a.ParentElement; } else if (currentData is AXmlTag) { AXmlTag tag = currentData as AXmlTag; if (tag.IsStartOrEmptyTag || tag.IsEndTag) { if (tag.NameSegment.EndOffset < offset) { description = XamlContextDescription.InTag; } else { description = XamlContextDescription.AtTag; } } else if (tag.IsComment) { description = XamlContextDescription.InComment; } else if (tag.IsCData) { description = XamlContextDescription.InCData; } active = tag.Parent as AXmlElement; } if (active != ancestors.FirstOrDefault()) { parent = ancestors.FirstOrDefault(); } else { parent = ancestors.Skip(1).FirstOrDefault(); } if (active == null) { active = parent; } var xAttribute = currentData as AXmlAttribute; var context = new XamlContext() { Description = description, ActiveElement = active, ParentElement = parent, RootElement = root, Ancestors = ancestors.AsReadOnly(), Attribute = xAttribute, InRoot = isRoot, AttributeValue = value, RawAttributeValue = attributeValue, ValueStartOffset = offsetFromValueStart, XmlnsDefinitions = xmlns, ParseInformation = info, IgnoredXmlns = ignored.AsReadOnly(), XamlNamespacePrefix = xamlNamespacePrefix }; return(context); }