Example #1
0
		/// <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;
		}
 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;
     }
 }
Example #3
0
 /// <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;
 }
		/// <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;
		}	
        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;
                    }
                }
            }
        }
 public static void PutXDocument(this OpenXmlPart part, XDocument document)
 {
     using (Stream partStream = part.GetStream(FileMode.Create, FileAccess.Write))
     using (XmlWriter partXmlWriter = XmlWriter.Create(partStream))
         document.Save(partXmlWriter);
     part.RemoveAnnotations<XDocument>();
     part.AddAnnotation(document);
 }
Example #7
0
        /// <summary>
        /// Adds line info to the given XObject.
        /// </summary>
        /// <param name="xmlObject">the XObject</param>
        /// <param name="lineInfo">a LineInfo object. This object is added as an annotation to <paramref name="xmlObject"/></param>
        public static void SetLineInfo(this XObject xmlObject, ABB.SrcML.LineInfo lineInfo) {
            if(null == xmlObject)
                throw new ArgumentNullException("xmlObject");

            xmlObject.AddAnnotation(lineInfo);
        }
		/// <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;
		}
Example #9
0
        // 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;
        }
Example #10
0
 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;
 }
Example #11
0
 public static void EnsureAnnotation(this XElement element)
 {
     var sfa = element.Annotation<XmlFileAnnotation>();
     if (sfa == null)
     {
         sfa = new XmlFileAnnotation();
         element.AddAnnotation(sfa);
     }
 }
Example #12
0
 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;
 }
Example #13
0
 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;
 }
Example #14
0
		/// <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;
		}