当前位置: 首页>>代码示例>>C#>>正文


C# ITypeSymbol.IsDefinedInSource方法代码示例

本文整理汇总了C#中ITypeSymbol.IsDefinedInSource方法的典型用法代码示例。如果您正苦于以下问题:C# ITypeSymbol.IsDefinedInSource方法的具体用法?C# ITypeSymbol.IsDefinedInSource怎么用?C# ITypeSymbol.IsDefinedInSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ITypeSymbol的用法示例。


在下文中一共展示了ITypeSymbol.IsDefinedInSource方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetInsertionPoints

		public static List<InsertionPoint> GetInsertionPoints (IReadonlyTextDocument data, MonoDevelop.Ide.TypeSystem.ParsedDocument parsedDocument, ITypeSymbol type, int part)
		{
			if (data == null)
				throw new ArgumentNullException (nameof (data));
			if (parsedDocument == null)
				throw new ArgumentNullException (nameof (parsedDocument));
			if (type == null)
				throw new ArgumentNullException (nameof (type));
			if (!type.IsDefinedInSource ())
				throw new ArgumentException ("The given type needs to be defined in source code.", nameof (type));

			// update type from parsed document, since this is always newer.
			//type = parsedDocument.GetInnermostTypeDefinition (type.GetLocation ()) ?? type;
			//var realStartLocation = data.OffsetToLocation (offset);
			var model = parsedDocument.GetAst<SemanticModel> ();
			return GetInsertionPoints (data, model, type, part);
		}
开发者ID:powerumc,项目名称:monodevelop_korean,代码行数:17,代码来源:InsertionPointService.cs

示例2: AddNewMember

		//		public static IUnresolvedMember AddCodeDomMember (MonoDevelop.Projects.Project project, IUnresolvedTypeDefinition type, CodeTypeMember newMember)
		//		{
		//			bool isOpen;
		//			var data = TextFileProvider.Instance.GetTextEditorData (type.Region.FileName, out isOpen);
		//			var parsedDocument = TypeSystemService.ParseFile (data.FileName, data.MimeType, data.Text);
		//			
		//			var insertionPoints = GetInsertionPoints (data, parsedDocument, type);
		//			
		//			var suitableInsertionPoint = GetSuitableInsertionPoint (insertionPoints, type, newMember);
		//			
		//			var dotNetProject = project as DotNetProject;
		//			if (dotNetProject == null) {
		//				LoggingService.LogError ("Only .NET projects are supported.");
		//				return null;
		//			}
		//			
		//			var generator = dotNetProject.LanguageBinding.GetCodeDomProvider ();
		//			StringWriter sw = new StringWriter ();
		//			var options = new CodeGeneratorOptions ();
		//			options.IndentString = data.GetLineIndent (type.Region.BeginLine) + "\t";
		//			if (newMember is CodeMemberMethod)
		//				options.BracingStyle = "C";
		//			generator.GenerateCodeFromMember (newMember, sw, options);
		//
		//			var code = sw.ToString ();
		//			if (!string.IsNullOrEmpty (code))
		//				suitableInsertionPoint.Insert (data, code);
		//			if (!isOpen) {
		//				try {
		//					File.WriteAllText (type.Region.FileName, data.Text);
		//				} catch (Exception e) {
		//					LoggingService.LogError (string.Format ("Failed to write file '{0}'.", type.Region.FileName), e);
		//					MessageService.ShowError (GettextCatalog.GetString ("Failed to write file '{0}'.", type.Region.FileName));
		//				}
		//			}
		//			var newDocument = TypeSystemService.ParseFile (data.FileName, data.MimeType, data.Text);
		//			return newDocument.ParsedFile.GetMember (suitableInsertionPoint.Location.Line, int.MaxValue);
		//		}

		public static async Task AddNewMember (Projects.Project project, ITypeSymbol type, Location part, SyntaxNode newMember, CancellationToken cancellationToken = default(CancellationToken))
		{
			if (project == null)
				throw new ArgumentNullException (nameof (project));
			if (type == null)
				throw new ArgumentNullException (nameof (type));
			if (newMember == null)
				throw new ArgumentNullException (nameof (newMember));
			if (!type.IsDefinedInSource ())
				throw new ArgumentException ("The given type needs to be defined in source code.", nameof (type));


			var ws = MonoDevelop.Ide.TypeSystem.TypeSystemService.GetWorkspace (project.ParentSolution);
			var projectId = ws.GetProjectId (project);
			var docId = ws.GetDocumentId (projectId, part.SourceTree.FilePath);

			var document = ws.GetDocument (docId, cancellationToken);

			var root = await document.GetSyntaxRootAsync (cancellationToken).ConfigureAwait (false);
			var typeDecl = (ClassDeclarationSyntax)root.FindNode (part.SourceSpan);

			// for some reason the reducer doesn't reduce this
			var systemVoid = newMember.DescendantNodesAndSelf ().OfType<QualifiedNameSyntax> ().FirstOrDefault (ma => ma.ToString () == "System.Void");

			if (systemVoid != null) newMember = newMember.ReplaceNode (systemVoid, SyntaxFactory.ParseTypeName ("void"));

			var newRoot = root.ReplaceNode (typeDecl, typeDecl.AddMembers ((MemberDeclarationSyntax)newMember.WithAdditionalAnnotations (Simplifier.Annotation, Formatter.Annotation)));
			document = document.WithSyntaxRoot (newRoot);
			var policy = project.Policies.Get<CSharpFormattingPolicy> ("text/x-csharp");
			var textPolicy = project.Policies.Get<TextStylePolicy> ("text/x-csharp");
			var projectOptions = policy.CreateOptions (textPolicy);

			document = await Formatter.FormatAsync (document, Formatter.Annotation, projectOptions, cancellationToken).ConfigureAwait (false);
			document = await Simplifier.ReduceAsync (document, Simplifier.Annotation, projectOptions, cancellationToken).ConfigureAwait (false);
			var text = await document.GetTextAsync (cancellationToken).ConfigureAwait (false);
			var newSolution = ws.CurrentSolution.WithDocumentText (docId, text);
			ws.TryApplyChanges (newSolution);
		}
开发者ID:zenek-y,项目名称:monodevelop,代码行数:77,代码来源:CodeGenerationService.cs

示例3: GetInsertionPoints

		public static List<InsertionPoint> GetInsertionPoints (IReadonlyTextDocument data, MonoDevelop.Ide.TypeSystem.ParsedDocument parsedDocument, ITypeSymbol type, int part)
		{
			if (data == null)
				throw new ArgumentNullException (nameof (data));
			if (parsedDocument == null)
				throw new ArgumentNullException (nameof (parsedDocument));
			if (type == null)
				throw new ArgumentNullException (nameof (type));
			if (!type.IsDefinedInSource ())
				throw new ArgumentException ("The given type needs to be defined in source code.", nameof (type));

			// update type from parsed document, since this is always newer.
			//type = parsedDocument.GetInnermostTypeDefinition (type.GetLocation ()) ?? type;
			List<InsertionPoint> result = new List<InsertionPoint> ();
			//var realStartLocation = data.OffsetToLocation (offset);
			var model = parsedDocument.GetAst<SemanticModel> ();
			type = model.GetEnclosingNamedType (part, default(CancellationToken)) as ITypeSymbol ?? type;
			var sourceSpan = new TextSpan (part, 0);

			var filePath = data.FileName;
			var declaringType = type.DeclaringSyntaxReferences.FirstOrDefault (dsr => dsr.SyntaxTree.FilePath == filePath && dsr.Span.Contains (sourceSpan)) ?? type.DeclaringSyntaxReferences.FirstOrDefault ();
			if (declaringType == null)
				return result;
			var openBraceToken = declaringType.GetSyntax ().ChildTokens ().FirstOrDefault (t => t.IsKind (SyntaxKind.OpenBraceToken));
			if (!openBraceToken.IsMissing) {
				var domLocation = data.OffsetToLocation (openBraceToken.SpanStart);
				result.Add (GetInsertionPosition (data, domLocation.Line, domLocation.Column));
				//			result.Add (GetInsertionPosition (data, realStartLocation.Line, realStartLocation.Column));
				result [0].LineBefore = NewLineInsertion.None;
			}
			foreach (var member in type.GetMembers ()) {
				if (member.IsImplicitlyDeclared || !member.IsDefinedInSource())
					continue;
				//var domLocation = member.BodyRegion.End;
				foreach (var loc in member.DeclaringSyntaxReferences) {
					if (loc.SyntaxTree.FilePath != declaringType.SyntaxTree.FilePath || !declaringType.Span.Contains (sourceSpan))
						continue;
					var domLocation = data.OffsetToLocation (loc.Span.End);

					if (domLocation.Line <= 0) {
						var lineSegment = data.GetLineByOffset (loc.Span.Start);
						if (lineSegment == null)
							continue;
						domLocation = new DocumentLocation (lineSegment.LineNumber, lineSegment.Length + 1);
					}
					result.Add (GetInsertionPosition (data, domLocation.Line, domLocation.Column));
					break;
				}
			}

			result [result.Count - 1].LineAfter = NewLineInsertion.None;
			CheckStartPoint (data, result [0], result.Count == 1);
			if (result.Count > 1) {
				result.RemoveAt (result.Count - 1); 
				NewLineInsertion insertLine;
				var typeSyntaxReference = type.DeclaringSyntaxReferences.FirstOrDefault (r => r.Span.Contains (sourceSpan));

				var lineBefore = data.GetLineByOffset (typeSyntaxReference.Span.End).PreviousLine;
				if (lineBefore != null && lineBefore.Length == lineBefore.GetIndentation (data).Length) {
					insertLine = NewLineInsertion.None;
				} else {
					insertLine = NewLineInsertion.Eol;
				}
				// search for line start
				var line = data.GetLineByOffset (typeSyntaxReference.Span.End);
				int col = typeSyntaxReference.Span.End - line.Offset;
				if (line != null) {
					var lineOffset = line.Offset;
					col = Math.Min (line.Length, col);
					while (lineOffset + col - 2 >= 0 && col > 1 && char.IsWhiteSpace (data.GetCharAt (lineOffset + col - 2)))
						col--;
				}
				result.Add (new InsertionPoint (new DocumentLocation (line.LineNumber, col), insertLine, NewLineInsertion.Eol));
				CheckEndPoint (data, result [result.Count - 1], result.Count == 1);
			}

//			foreach (var region in parsedDocument.UserRegions.Where (r => type.BodyRegion.IsInside (r.Region.Begin))) {
//				result.Add (new InsertionPoint (new DocumentLocation (region.Region.BeginLine + 1, 1), NewLineInsertion.Eol, NewLineInsertion.Eol));
//				result.Add (new InsertionPoint (new DocumentLocation (region.Region.EndLine, 1), NewLineInsertion.Eol, NewLineInsertion.Eol));
//				result.Add (new InsertionPoint (new DocumentLocation (region.Region.EndLine + 1, 1), NewLineInsertion.Eol, NewLineInsertion.Eol));
//			}
			result.Sort ((left, right) => left.Location.CompareTo (right.Location));
			//foreach (var res in result)
			//	Console.WriteLine (res);
			return result;
		}
开发者ID:pabloescribanoloza,项目名称:monodevelop,代码行数:86,代码来源:InsertionPointService.cs


注:本文中的ITypeSymbol.IsDefinedInSource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。