public void Run(XElement element, MSBuildLanguageElement resolvedElement, string filename, ITextSource textDocument, MSBuildDocument document, int offset = 0, int length = 0) { Filename = filename; Document = document; Extension = System.IO.Path.GetExtension(filename); //HACK: we should really use the ITextSource directly, but since the XML parser positions are //currently line/col, we need a TextDocument to convert to offsets TextDocument = textDocument as IReadonlyTextDocument ?? TextEditorFactory.CreateNewReadonlyDocument( textDocument, filename, MSBuildTextEditorExtension.MSBuildMimeType ); range = new DocumentRegion( TextDocument.OffsetToLocation(offset), length > 0 ? TextDocument.OffsetToLocation(length + offset) : new DocumentLocation(int.MaxValue, int.MaxValue)); if (resolvedElement != null) { VisitResolvedElement(element, resolvedElement); } else if (element != null) { ResolveAndVisit(element, null); } }
TextEditor GetDocument() { if (cachedEditor == null || cachedEditor.FileName != FileName) { var content = FileProvider.ReadString(); cachedEditor?.Dispose(); cachedEditor = TextEditorFactory.CreateNewEditor(TextEditorFactory.CreateNewReadonlyDocument(new Core.Text.StringTextSource(content.ReadToEnd()), FileName, DesktopService.GetMimeTypeForUri(FileName))); } return(cachedEditor); }
public static MSBuildNavigationResult GetNavigation( MSBuildRootDocument doc, DocumentLocation location, MSBuildResolveResult rr) { if (rr == null) { return(null); } //HACK: we should really use the ITextSource directly, but since the XML parser positions are //currently line/col, we need a TextDocument to convert to offsets var textDocument = doc.Text as IReadonlyTextDocument ?? TextEditorFactory.CreateNewReadonlyDocument( doc.Text, doc.Filename, MSBuildTextEditorExtension.MSBuildMimeType ); var annotations = GetAnnotationsAtLocation <NavigationAnnotation> (doc, location); var firstAnnotation = annotations.FirstOrDefault(); if (firstAnnotation != null) { var beginOffset = textDocument.LocationToOffset(firstAnnotation.Region.Begin); var endOffset = textDocument.LocationToOffset(firstAnnotation.Region.End); return(new MSBuildNavigationResult( annotations.Select(a => a.Path).ToArray(), beginOffset, endOffset - beginOffset + 1 )); } if (rr.ReferenceKind == MSBuildReferenceKind.Target) { return(new MSBuildNavigationResult( MSBuildReferenceKind.Target, (string)rr.Reference, rr.ReferenceOffset, rr.ReferenceLength )); } if (rr.ReferenceKind == MSBuildReferenceKind.FileOrFolder) { return(new MSBuildNavigationResult( (string[])rr.Reference, rr.ReferenceOffset, rr.ReferenceLength )); } if (rr.ReferenceKind == MSBuildReferenceKind.Task) { var task = doc.GetTask((string)rr.Reference); if (task.DeclaredInFile != null) { return(new MSBuildNavigationResult( MSBuildReferenceKind.Task, (string)rr.Reference, rr.ReferenceOffset, rr.ReferenceLength, task.DeclaredInFile, task.DeclaredAtLocation )); } } return(null); }
static string AddIndent(string text, string indent) { var doc = TextEditorFactory.CreateNewReadonlyDocument(new StringTextSource(text), ""); var result = StringBuilderCache.Allocate(); foreach (var line in doc.GetLines()) { result.Append(indent); result.Append(doc.GetTextAt(line.SegmentIncludingDelimiter)); } return(StringBuilderCache.ReturnAndFree(result)); }
TextEditor GetDocument(SearchResult result) { TextEditor doc; if (!documents.TryGetValue(result.FileName, out doc)) { var content = result.FileProvider.ReadString(); if (content == null) { return(null); } doc = TextEditorFactory.CreateNewEditor(TextEditorFactory.CreateNewReadonlyDocument(new StringTextSource(content.ReadToEnd()), result.FileName, DesktopService.GetMimeTypeForUri(result.FileName))); doc.FileName = result.FileName; documents [result.FileName] = doc; } return(doc); }
static DocumentLocation GetEndLocationAfterRemoval(DocumentLocation startLocation, TextChange textChange) { var document = TextEditorFactory.CreateNewReadonlyDocument(textChange.RemovedText, "a.txt"); if (document.LineCount > 1) { int line = startLocation.Line + document.LineCount - 1; IDocumentLine lastLine = document.GetLine(document.LineCount); int column = lastLine.LengthIncludingDelimiter + 1; return(new DocumentLocation(line, column)); } else { int column = startLocation.Column + textChange.RemovalLength; return(new DocumentLocation(startLocation.Line, column)); } }
TextEditor GetDocument() { var fileProvider = FileProvider; if (cachedEditor == null || cachedEditor.IsDisposed || cachedEditor.FileName != FileName || cachedEditorFileProvider != fileProvider) { if (fileProvider == null) { throw new InvalidOperationException("FileProvider == null"); } var content = FileProvider.ReadString(); if (content == null) { throw new InvalidOperationException("FileProvider.ReadString () == null"); } cachedEditor?.Dispose(); cachedEditor = TextEditorFactory.CreateNewEditor(TextEditorFactory.CreateNewReadonlyDocument(new Core.Text.StringTextSource(content.ReadToEnd()), FileName, IdeServices.DesktopService.GetMimeTypeForUri(FileName))); cachedEditorFileProvider = FileProvider; } return(cachedEditor); }
public void Run(XElement element, MSBuildLanguageElement resolvedElement, string filename, ITextSource textDocument, MSBuildDocument document) { Filename = filename; Document = document; //HACK: we should really use the ITextSource directly, but since the XML parser positions are //currently line/col, we need a TextDocument to convert to offsets TextDocument = textDocument as IReadonlyTextDocument ?? TextEditorFactory.CreateNewReadonlyDocument( textDocument, filename, MSBuildTextEditorExtension.MSBuildMimeType ); if (resolvedElement != null) { VisitResolvedElement(element, resolvedElement); } else if (element != null) { ResolveAndVisit(element, null); } }
public override System.Threading.Tasks.Task <ParsedDocument> Parse(ParseOptions parseOptions, System.Threading.CancellationToken cancellationToken) { var fileName = parseOptions.FileName; ParsedTemplate template = new ParsedTemplate(fileName); var readOnlyDoc = TextEditorFactory.CreateNewReadonlyDocument(parseOptions.Content, fileName); try { var tk = new Tokeniser(fileName, readOnlyDoc.Text); template.ParseWithoutIncludes(tk); } catch (ParserException ex) { template.LogError(ex.Message, ex.Location); } var errors = new List <Error> (); foreach (System.CodeDom.Compiler.CompilerError err in template.Errors) { errors.Add(new Error(err.IsWarning ? ErrorType.Warning : ErrorType.Error, err.ErrorText, new DocumentLocation(err.Line, err.Column))); } var doc = new T4ParsedDocument(fileName, template.RawSegments, errors); doc.Flags |= ParsedDocumentFlags.NonSerializable; return(System.Threading.Tasks.Task.FromResult((ParsedDocument)doc)); }
// Returns a stream with the content of the file. // project and language parameters are optional public virtual async Task <Stream> CreateFileContentAsync(SolutionFolderItem policyParent, Project project, string language, string fileName, string identifier) { var model = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, identifier, fileName); //HACK: for API compat, CreateContent just gets the override, not the base model // but ProcessContent gets the entire model string content = CreateContent(project, model.OverrideTags, language); content = ProcessContent(content, model); string mime = DesktopService.GetMimeTypeForUri(fileName); var formatter = !string.IsNullOrEmpty(mime) ? CodeFormatterService.GetFormatter(mime) : null; if (formatter != null) { var document = TextEditorFactory.CreateNewReadonlyDocument(new StringTextSource(content), fileName); // Avoid possible UI thread deadlock in CSharpFormatter by running the formatter with Task.Run. var formatted = await Task.Run(() => formatter.Format(policyParent?.Policies, document)); if (formatted != null) { content = formatted.Text; } } var ms = new MemoryStream(); Encoding encoding = null; TextStylePolicy textPolicy = policyParent != null?policyParent.Policies.Get <TextStylePolicy> (mime ?? "text/plain") : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> (mime ?? "text/plain"); string eolMarker = TextStylePolicy.GetEolMarker(textPolicy.EolMarker); var ctx = await EditorConfigService.GetEditorConfigContext(fileName); if (ctx != null) { ctx.CurrentConventions.UniversalConventions.TryGetEncoding(out encoding); if (ctx.CurrentConventions.UniversalConventions.TryGetLineEnding(out string lineEnding)) { eolMarker = lineEnding; } } if (encoding == null) { encoding = System.Text.Encoding.UTF8; } var bom = encoding.GetPreamble(); if (bom != null && bom.Length > 0) { ms.Write(bom, 0, bom.Length); } byte[] data; if (AddStandardHeader) { string header = StandardHeaderService.GetHeader(policyParent, fileName, true); data = encoding.GetBytes(header); ms.Write(data, 0, data.Length); } var doc = TextEditorFactory.CreateNewDocument(); doc.Text = content; byte[] eolMarkerBytes = encoding.GetBytes(eolMarker); bool convertTabsToSpaces = textPolicy.TabsToSpaces; int tabWidth = textPolicy.TabWidth; if (ctx != null) { if (ctx.CurrentConventions.UniversalConventions.TryGetIndentStyle(out Microsoft.VisualStudio.CodingConventions.IndentStyle result)) { convertTabsToSpaces = result == Microsoft.VisualStudio.CodingConventions.IndentStyle.Spaces; } if (ctx.CurrentConventions.UniversalConventions.TryGetTabWidth(out int editorConfigTabWidth)) { tabWidth = editorConfigTabWidth; } } var tabToSpaces = convertTabsToSpaces ? new string (' ', tabWidth) : null; IDocumentLine lastLine = null; foreach (var line in doc.GetLines()) { var lineText = doc.GetTextAt(line.Offset, line.Length); if (tabToSpaces != null) { lineText = lineText.Replace("\t", tabToSpaces); } if (line.LengthIncludingDelimiter > 0) { data = encoding.GetBytes(lineText); ms.Write(data, 0, data.Length); ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length); } lastLine = line; } if (ctx != null && lastLine != null && lastLine.Length > 0) { if (ctx.CurrentConventions.UniversalConventions.TryGetRequireFinalNewline(out bool requireNewLine)) { if (requireNewLine) { ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length); } } } ms.Position = 0; return(ms); }