/// <summary> /// Resolves the <see cref="ExportProvider"/> for the given <see cref="XDocument"/>. /// </summary> /// <param name="self"></param> /// <returns></returns> public static ExportProvider Exports(this XDocument self) { Contract.Requires<ArgumentNullException>(self != null); Contract.Ensures(Contract.Result<ExportProvider>() != null); return self.Annotation<ExportProvider>(); }
public static XDocument GetXDocument(this OpenXmlPart part) { try { XDocument partXDocument = part.Annotation<XDocument>(); if (partXDocument != null) return partXDocument; using (Stream partStream = part.GetStream()) { if (partStream.Length == 0) { partXDocument = new XDocument(); partXDocument.Declaration = new XDeclaration("1.0", "UTF-8", "yes"); } else using (XmlReader partXmlReader = XmlReader.Create(partStream)) partXDocument = XDocument.Load(partXmlReader); } part.AddAnnotation(partXDocument); return partXDocument; } catch (Exception e) { throw e; } }
/// <summary> /// Returns a full C# syntax tree resolver which is shared between semantic highlighting, source analysis and refactoring. /// For code analysis tasks this should be used instead of generating an own resolver. Only exception is if a local resolving is done using a /// resolve navigator. /// Note: The shared resolver is fully resolved. /// </summary> public static Task<CSharpAstResolver> GetSharedResolver (this Document document) { var parsedDocument = document.ParsedDocument; if (parsedDocument == null) return null; var unit = parsedDocument.GetAst<SyntaxTree> (); var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile; if (unit == null || parsedFile == null) return null; var resolverAnnotation = document.Annotation<ResolverAnnotation> (); if (resolverAnnotation != null) { if (resolverAnnotation.ParsedFile == parsedFile) return resolverAnnotation.Task; document.RemoveAnnotations<ResolverAnnotation> (); } var resolveTask = Task.Factory.StartNew (delegate { var result = new CSharpAstResolver (document.Compilation, unit, parsedFile); result.ApplyNavigator (new ConstantModeResolveVisitorNavigator (ResolveVisitorNavigationMode.Resolve, null)); return result; }); document.AddAnnotation (new ResolverAnnotation { Task = resolveTask, ParsedFile = parsedFile }); return resolveTask; }
/// <summary> /// Returns a full C# syntax tree resolver which is shared between semantic highlighting, source analysis and refactoring. /// For code analysis tasks this should be used instead of generating an own resolver. Only exception is if a local resolving is done using a /// resolve navigator. /// Note: The shared resolver is fully resolved. /// </summary> public static Task<CSharpAstResolver> GetSharedResolver (this Document document) { var parsedDocument = document.ParsedDocument; if (parsedDocument == null || document.IsProjectContextInUpdate || document.Project != null && !(document.Project is DotNetProject)) return null; var unit = parsedDocument.GetAst<SyntaxTree> (); var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile; if (unit == null || parsedFile == null) return null; var compilation = document.Compilation; var resolverAnnotation = document.Annotation<ResolverAnnotation> (); if (resolverAnnotation != null) { if (resolverAnnotation.ParsedFile == parsedFile) return resolverAnnotation.Task; if (resolverAnnotation.SharedTokenSource != null) resolverAnnotation.SharedTokenSource.Cancel (); document.RemoveAnnotations<ResolverAnnotation> (); } var tokenSource = new CancellationTokenSource (); var token = tokenSource.Token; var resolveTask = Task.Factory.StartNew (delegate { try { using (var timer = ResolveCounter.BeginTiming ()) { var result = new CSharpAstResolver (compilation, unit, parsedFile); result.ApplyNavigator (new ConstantModeResolveVisitorNavigator (ResolveVisitorNavigationMode.Resolve, null), token); return result; } } catch (OperationCanceledException) { return null; } catch (Exception e) { LoggingService.LogError ("Error while creating the resolver.", e); return null; } }, token); var wrapper = resolveTask.ContinueWith (t => { if (t.IsCanceled) return null; if (t.IsFaulted) { var ex = t.Exception.Flatten ().InnerException; if (!(ex is TaskCanceledException)) LoggingService.LogWarning ("Exception while getting shared AST resolver.", ex); return null; } return t.Result; }, TaskContinuationOptions.ExecuteSynchronously); document.AddAnnotation (new ResolverAnnotation { Task = wrapper, ParsedFile = parsedFile, SharedTokenSource = tokenSource }); return wrapper; }
public static IXmlSchemaInfo GetSchemaInfo(this XAttribute source) { if (source == null) { throw new ArgumentNullException("source"); } return source.Annotation<IXmlSchemaInfo>(); }
public static XDocument GetXDocument(this OpenXmlPart part, out XmlNamespaceManager namespaceManager) { XDocument partXDocument = part.Annotation<XDocument>(); namespaceManager = part.Annotation<XmlNamespaceManager>(); if (partXDocument != null) { if (namespaceManager != null) return partXDocument; namespaceManager = GetManagerFromXDocument(partXDocument); part.AddAnnotation(namespaceManager); return partXDocument; } using (Stream partStream = part.GetStream()) { if (partStream.Length == 0) { partXDocument = new XDocument(); partXDocument.Declaration = new XDeclaration("1.0", "UTF-8", "yes"); part.AddAnnotation(partXDocument); return partXDocument; } else { using (XmlReader partXmlReader = XmlReader.Create(partStream)) { partXDocument = XDocument.Load(partXmlReader); XmlNameTable nameTable = partXmlReader.NameTable; namespaceManager = new XmlNamespaceManager(nameTable); part.AddAnnotation(partXDocument); part.AddAnnotation(namespaceManager); return partXDocument; } } } }
/// <summary> /// Returns a full C# syntax tree resolver which is shared between semantic highlighting, source analysis and refactoring. /// For code analysis tasks this should be used instead of generating an own resolver. Only exception is if a local resolving is done using a /// resolve navigator. /// </summary> public static CSharpAstResolver GetSharedResolver (this Document document) { var parsedDocument = document.ParsedDocument; if (parsedDocument == null) return null; var unit = parsedDocument.GetAst<SyntaxTree> (); var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile; if (unit == null || parsedFile == null) return null; var currentResolver = document.Annotation<CSharpAstResolver> (); if (currentResolver != null) { if (currentResolver.UnresolvedFile == parsedFile) return currentResolver; document.RemoveAnnotations<CSharpAstResolver> (); } var result = new CSharpAstResolver (document.Compilation, unit, parsedFile); document.AddAnnotation (result); return result; }
/// <summary> /// Gets the XDocument for a part /// </summary> public static XDocument GetXDocument(this OpenXmlPart part) { XDocument xdoc = part.Annotation<XDocument>(); if (xdoc != null) return xdoc; try { using (StreamReader sr = new StreamReader(part.GetStream())) using (XmlReader xr = XmlReader.Create(sr)) { xdoc = XDocument.Load(xr); xdoc.Changed += ElementChanged; xdoc.Changing += ElementChanged; } } catch (XmlException) { xdoc = new XDocument(); xdoc.AddAnnotation(new ChangedSemaphore()); } part.AddAnnotation(xdoc); return xdoc; }
public static IXmlSchemaInfo GetSchemaInfo (this XElement element) { return element.Annotation<IXmlSchemaInfo> (); }
public static IXmlSchemaInfo GetSchemaInfo (this XAttribute source) { return source.Annotation<IXmlSchemaInfo> (); }
public static IXmlSchemaInfo GetSchemaInfo (this XElement source) { return source.Annotation<IXmlSchemaInfo> (); }
public static BlockContentInfo GetParagraphInfo(this XElement contentElement) { BlockContentInfo paragraphInfo = contentElement.Annotation<BlockContentInfo>(); if (paragraphInfo != null) return paragraphInfo; InitializeParagraphInfo(contentElement.Parent); return contentElement.Annotation<BlockContentInfo>(); }
/// <summary> /// Returns a full C# syntax tree resolver which is shared between semantic highlighting, source analysis and refactoring. /// For code analysis tasks this should be used instead of generating an own resolver. Only exception is if a local resolving is done using a /// resolve navigator. /// Note: The shared resolver is fully resolved. /// </summary> public static TaskWrapper GetSharedResolver (this Document document) { var parsedDocument = document.ParsedDocument; if (parsedDocument == null || document.IsProjectContextInUpdate) return null; var unit = parsedDocument.GetAst<SyntaxTree> (); var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile; if (unit == null || parsedFile == null) return null; var compilation = document.Compilation; var resolverAnnotation = document.Annotation<ResolverAnnotation> (); if (resolverAnnotation != null) { if (resolverAnnotation.ParsedFile == parsedFile) return resolverAnnotation.Task; if (resolverAnnotation.SharedTokenSource != null) resolverAnnotation.SharedTokenSource.Cancel (); document.RemoveAnnotations<ResolverAnnotation> (); } var tokenSource = new CancellationTokenSource (); var token = tokenSource.Token; var resolveTask = Task.Factory.StartNew (delegate { try { using (var timer = ResolveCounter.BeginTiming ()) { var result = new CSharpAstResolver (compilation, unit, parsedFile); result.ApplyNavigator (new ConstantModeResolveVisitorNavigator (ResolveVisitorNavigationMode.Resolve, null), token); return result; } } catch (OperationCanceledException) { return null; } catch (Exception e) { LoggingService.LogError ("Error while creating the resolver.", e); return null; } }, token); var wrapper = new TaskWrapper (resolveTask); document.AddAnnotation (new ResolverAnnotation { Task = wrapper, ParsedFile = parsedFile, SharedTokenSource = tokenSource }); return wrapper; }
// Used to track changes to parts /// <summary> /// Gets the XDocument for a part /// </summary> public static XDocument GetXDocumentWithTracking(this OpenXmlPart part) { var xdoc = part.Annotation<XDocument>(); if (xdoc != null) return xdoc; try { using (var sr = new StreamReader(part.GetStream())) using (XmlReader xr = XmlReader.Create(sr)) { xdoc = XDocument.Load(xr); xdoc.Changed += ElementChanged; xdoc.Changing += ElementChanged; } } catch (XmlException) { var xdec = new XDeclaration("1.0", "UTF-8", "yes"); xdoc = new XDocument(xdec); xdoc.AddAnnotation(new ChangedSemaphore()); } part.AddAnnotation(xdoc); return xdoc; }
public static XDocument GetXDocument(this OpenXmlPart part) { var partXDocument = part.Annotation<XDocument>(); if (partXDocument != null) return partXDocument; using (Stream partStream = part.GetStream()) using (XmlReader partXmlReader = XmlReader.Create(partStream)) partXDocument = XDocument.Load(partXmlReader); part.AddAnnotation(partXDocument); return partXDocument; }
public static IXmlSchemaInfo GetSchemaInfo (this XAttribute attribute) { return attribute.Annotation<IXmlSchemaInfo> (); }
/// <summary> /// <para>Annotation to display next to the button.</para> /// </summary> /// <param name="widget">Widget to call method on.</param> /// <param name="annotation">Annotation for the button.</param> /// <returns>Reference to provided <paramref name="widget"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="widget"/> is a <c>null</c> reference.</exception> /// <seealso cref="IGooglePlusOneButtonWidget.Annotation(string)"/> public static IGooglePlusOneButtonWidget Annotation(this IGooglePlusOneButtonWidget widget, GooglePlusOneButtonAnnotation annotation) { Assertion.NotNull(widget); return widget.Annotation(annotation.ToString().ToLowerInvariant()); }
public static void EnsureAnnotation(this XElement element) { var sfa = element.Annotation<XmlFileAnnotation>(); if (sfa == null) { sfa = new XmlFileAnnotation(); element.AddAnnotation(sfa); } }
public static void SetTextRange(this XElement element, ElementTextRange textRange) { var sfa = element.Annotation<XmlFileAnnotation>(); if (sfa == null) { sfa = new XmlFileAnnotation(); element.AddAnnotation(sfa); } sfa.TextRange = textRange; }
public static ElementTextRange GetTextRange(this XElement element) { var sfa = element.Annotation<XmlFileAnnotation>(); if (sfa == null) { return null; } return sfa.TextRange; }
public static void SetTextRange(this XObject attribute, TextRange textRange) { var saa = attribute.Annotation<XmlAttributeAnnotation>(); if (saa == null) { saa = new XmlAttributeAnnotation(); attribute.AddAnnotation(saa); } saa.TextRange = textRange; }
public static TextRange GetTextRange(this XObject attribute) { var saa = attribute.Annotation<XmlAttributeAnnotation>(); if (saa == null) { return null; } return saa.TextRange; }