public override Task <TextEditContainer?> Handle(DocumentFormattingParams request, CancellationToken cancellationToken) { CompilationContext?context = this.compilationManager.GetCompilation(request.TextDocument.Uri); if (context == null) { // we have not yet compiled this document, which shouldn't really happen this.logger.LogError("Document formatting request arrived before file {Uri} could be compiled.", request.TextDocument.Uri); return(Task.FromResult <TextEditContainer?>(null)); } long indentSize = request.Options.TabSize; IndentKindOption indentKindOption = request.Options.InsertSpaces ? IndentKindOption.Space : IndentKindOption.Tab; bool insertFinalNewline = request.Options.ContainsKey("insertFinalNewline") && request.Options.InsertFinalNewline; ProgramSyntax programSyntax = context.ProgramSyntax; PrettyPrintOptions options = new PrettyPrintOptions(NewlineOption.Auto, indentKindOption, indentSize, insertFinalNewline); string?output = PrettyPrinter.PrintProgram(programSyntax, options); if (output == null) { return(Task.FromResult <TextEditContainer?>(null)); } return(Task.FromResult <TextEditContainer?>(new TextEditContainer(new TextEdit { Range = programSyntax.Span.ToRange(context.LineStarts), NewText = output }))); }
public PrettyPrintOptions( NewlineOption newlineOption, IndentKindOption indentKindOption, long indentSize, bool insertFinalNewLine) { this.NewlineOption = newlineOption; this.IndentKindOption = indentKindOption; this.IndentSize = (int)Math.Min(MaxIndentSize, Math.Max(MinIndentSize, indentSize)); this.InsertFinalNewline = insertFinalNewLine; }