MonoDevelop.Refactoring.InsertionPointService.GetInsertionPoints C# (CSharp) Method

GetInsertionPoints() static private method

static private GetInsertionPoints ( IReadonlyTextDocument data, ITypeSymbol type, List result, TextSpan sourceSpan, Microsoft.CodeAnalysis.SyntaxReference declaringType ) : List
data IReadonlyTextDocument
type ITypeSymbol
result List
sourceSpan TextSpan
declaringType Microsoft.CodeAnalysis.SyntaxReference
return List
		static List<InsertionPoint> GetInsertionPoints (IReadonlyTextDocument data, ITypeSymbol type, List<InsertionPoint> result, TextSpan sourceSpan, SyntaxReference declaringType)
		{
			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;
		}

Same methods

InsertionPointService::GetInsertionPoints ( IReadonlyTextDocument data, MonoDevelop parsedDocument, ITypeSymbol type, Location location ) : List
InsertionPointService::GetInsertionPoints ( IReadonlyTextDocument data, MonoDevelop parsedDocument, ITypeSymbol type, int part ) : List
InsertionPointService::GetInsertionPoints ( IReadonlyTextDocument data, Microsoft.CodeAnalysis.SemanticModel model, ITypeSymbol type, int part ) : List

Usage Example

        public static async Task InsertMemberWithCursor(string operation, Projects.Project project, ITypeSymbol type, Location part, SyntaxNode newMember, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (newMember == null)
            {
                throw new ArgumentNullException(nameof(newMember));
            }
            var doc = await IdeApp.Workbench.OpenDocument(part.SourceTree.FilePath, project, true);

            var textView = await doc.GetContentWhenAvailable <ITextView> (cancellationToken);

            await doc.DocumentContext.UpdateParseDocument();

            var document = doc.DocumentContext.AnalysisDocument;

            if (document == null)
            {
                LoggingService.LogError("Can't find document to insert member (fileName:" + part.SourceTree.FilePath + ")");
                return;
            }
            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, insertedMemberAnnotation)));

            var projectOptions = await document.GetOptionsAsync(cancellationToken);

            document = document.WithSyntaxRoot(newRoot);
            document = await Formatter.FormatAsync(document, Formatter.Annotation, projectOptions, cancellationToken).ConfigureAwait(false);

            document = await Simplifier.ReduceAsync(document, Simplifier.Annotation, projectOptions, cancellationToken).ConfigureAwait(false);

            root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var node  = root.GetAnnotatedNodes(insertedMemberAnnotation).Single();
            var model = await doc.DocumentContext.AnalysisDocument.GetSemanticModelAsync(cancellationToken);

            Application.Invoke((o, args) => {
                var editor = doc.Editor;
                if (editor == null)
                {
                    // bypass the insertion point selection UI for the new editor
                    document.Project.Solution.Workspace.TryApplyChanges(document.Project.Solution);
                    var editorOperationsFactoryService = CompositionManager.Instance.GetExportedValue <IEditorOperationsFactoryService> ();
                    var editorOperations = editorOperationsFactoryService.GetEditorOperations(textView);
                    var point            = new Microsoft.VisualStudio.Text.VirtualSnapshotPoint(textView.TextSnapshot, node.SpanStart);
                    editorOperations.SelectAndMoveCaret(point, point, TextSelectionMode.Stream, EnsureSpanVisibleOptions.AlwaysCenter);
                    return;
                }

                var insertionPoints = InsertionPointService.GetInsertionPoints(
                    editor,
                    model,
                    type,
                    part.SourceSpan.Start
                    );
                var options = new InsertionModeOptions(
                    operation,
                    insertionPoints,
                    point => {
                    if (!point.Success)
                    {
                        return;
                    }
                    var text = node.ToString();
                    point.InsertionPoint.Insert(editor, doc.DocumentContext, text);
                }
                    );

                editor.StartInsertionMode(options);
            });
        }
All Usage Examples Of MonoDevelop.Refactoring.InsertionPointService::GetInsertionPoints