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); }
public static List <InsertionPoint> GetInsertionPoints(IReadonlyTextDocument data, MonoDevelop.Ide.TypeSystem.ParsedDocument parsedDocument, ITypeSymbol type, Location location) { return(GetInsertionPoints(data, parsedDocument, type, location.SourceSpan.Start)); }
public static List <InsertionPoint> GetInsertionPoints(TextEditorData data, ParsedDocument parsedDocument, IUnresolvedTypeDefinition type) { if (data == null) { throw new ArgumentNullException("data"); } if (parsedDocument == null) { throw new ArgumentNullException("parsedDocument"); } if (type == null) { throw new ArgumentNullException("type"); } // update type from parsed document, since this is always newer. //type = parsedDocument.GetInnermostTypeDefinition (type.GetLocation ()) ?? type; List <InsertionPoint> result = new List <InsertionPoint> (); int offset = data.LocationToOffset(type.Region.Begin); if (offset < 0 || type.BodyRegion.IsEmpty) { return(result); } while (offset < data.Length && data.GetCharAt(offset) != '{') { offset++; } var realStartLocation = data.OffsetToLocation(offset); result.Add(GetInsertionPosition(data.Document, realStartLocation.Line, realStartLocation.Column)); result [0].LineBefore = NewLineInsertion.None; foreach (var member in type.Members) { TextLocation domLocation = member.BodyRegion.End; if (domLocation.Line <= 0) { DocumentLine lineSegment = data.GetLine(member.Region.BeginLine); if (lineSegment == null) { continue; } domLocation = new TextLocation(member.Region.BeginLine, lineSegment.Length + 1); } result.Add(GetInsertionPosition(data.Document, domLocation.Line, domLocation.Column)); } foreach (var nestedType in type.NestedTypes) { TextLocation domLocation = nestedType.BodyRegion.End; if (domLocation.Line <= 0) { DocumentLine lineSegment = data.GetLine(nestedType.Region.BeginLine); if (lineSegment == null) { continue; } domLocation = new TextLocation(nestedType.Region.BeginLine, lineSegment.Length + 1); } result.Add(GetInsertionPosition(data.Document, domLocation.Line, domLocation.Column)); } result [result.Count - 1].LineAfter = NewLineInsertion.None; CheckStartPoint(data.Document, result [0], result.Count == 1); if (result.Count > 1) { result.RemoveAt(result.Count - 1); NewLineInsertion insertLine; var lineBefore = data.GetLine(type.BodyRegion.EndLine - 1); if (lineBefore != null && lineBefore.Length == lineBefore.GetIndentation(data.Document).Length) { insertLine = NewLineInsertion.None; } else { insertLine = NewLineInsertion.Eol; } // search for line start int col = type.BodyRegion.EndColumn - 1; var line = data.GetLine(type.BodyRegion.EndLine); 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(type.BodyRegion.EndLine, col), insertLine, NewLineInsertion.Eol)); CheckEndPoint(data.Document, 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); }
public ParsedDocumentProjection(ParsedDocument parsedDocument, IReadOnlyList <Projection> projections, DisabledProjectionFeatures disabledProjectionFeatures = DisabledProjectionFeatures.None) { this.ParsedDocument = parsedDocument; this.Projections = projections; this.DisabledProjectionFeatures = disabledProjectionFeatures; }
/// <summary> /// Gets an up to date partial projection used by code completion. /// </summary> public virtual Task <IReadOnlyList <Projection> > GetPartialProjectionsAsync(DocumentContext ctx, TextEditor editor, ParsedDocument currentParsedDocument, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotSupportedException(); }
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)); }