All files / src/frontend FormattingProvider.ts

0% Statements 0/10
100% Branches 0/0
0% Functions 0/2
0% Lines 0/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                                                               
/*
 * This file is released under the MIT license.
 * Copyright (c) 2016, 2022 Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
import { AntlrFacade } from "../backend/facade";
import {
    DocumentRangeFormattingEditProvider, TextDocument, FormattingOptions, CancellationToken, ProviderResult, TextEdit,
    Range, workspace,
} from "vscode";
 
export class AntlrFormattingProvider implements DocumentRangeFormattingEditProvider {
    public constructor(private backend: AntlrFacade) { }
 
    public provideDocumentRangeFormattingEdits(document: TextDocument, range: Range, _options: FormattingOptions,
        _token: CancellationToken): ProviderResult<TextEdit[]> {
 
        let start = document.offsetAt(range.start);
        let end = document.offsetAt(range.end) - 1; // Make the end inclusive.
 
        const formatOptions = workspace.getConfiguration("antlr4.format");
        let text = "";
        [text, start, end] = this.backend.formatGrammar(document.fileName, Object.assign({}, formatOptions), start,
            end);
        const resultRange = range.with(document.positionAt(start), document.positionAt(end + 1));
 
        return [TextEdit.replace(resultRange, text)];
    }
}