Path: blob/main/extensions/copilot/src/platform/editing/common/notebookDocumentSnapshot.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type { NotebookDocument, TextLine, Uri } from 'vscode';6import { isNumber, isString } from '../../../util/vs/base/common/types';7import { isUriComponents, UriComponents } from '../../../util/vs/base/common/uri';8import { Position, Range, Selection } from '../../../vscodeTypes';9import { getAlternativeNotebookDocumentProvider } from '../../notebook/common/alternativeContent';10import { AlternativeNotebookDocument } from '../../notebook/common/alternativeNotebookDocument';11import { getDefaultLanguage } from '../../notebook/common/helpers';1213export interface INotebookDocumentSnapshotJSON {14readonly type: 'notebook';15readonly uri: UriComponents;16readonly _text: string;17readonly languageId: string;18readonly version: number;19readonly alternativeFormat: 'json' | 'xml' | 'text';20}2122export function isNotebookDocumentSnapshotJSON(thing: any): thing is INotebookDocumentSnapshotJSON {23if (!thing || typeof thing !== 'object') {24return false;25}26return thing.type === 'notebook' && isUriComponents(thing.uri) && isString(thing._text) &&27isString(thing.languageId) && isNumber(thing.version) && isString(thing.alternativeFormat);28}2930export class NotebookDocumentSnapshot {31static create(doc: NotebookDocument, format: 'json' | 'xml' | 'text'): NotebookDocumentSnapshot {32const uri = doc.uri;33const version = doc.version;3435const alternativeDocument = getAlternativeNotebookDocumentProvider(format).getAlternativeDocument(doc);36return new NotebookDocumentSnapshot(doc, uri, version, format, alternativeDocument);37}38static fromNewText(text: string, doc: NotebookDocumentSnapshot) {39const alternativeDocument = getAlternativeNotebookDocumentProvider(doc.alternativeFormat).getAlternativeDocumentFromText(text, doc.document);40const nd = new NotebookDocumentSnapshot(doc.document, doc.uri, doc.version, doc.alternativeFormat, alternativeDocument);41return nd;42}43static fromJSON(doc: NotebookDocument, json: INotebookDocumentSnapshotJSON): NotebookDocumentSnapshot {44// TODO@DonJayamanne45return NotebookDocumentSnapshot.create(doc, json.alternativeFormat);46}4748readonly type = 'notebook';49readonly document: NotebookDocument;50readonly uri: Uri;51readonly version: number;52readonly languageId: string;535455private constructor(doc: NotebookDocument, uri: Uri, version: number, public readonly alternativeFormat: 'json' | 'xml' | 'text', private readonly _alternativeDocument: AlternativeNotebookDocument) {56this.document = doc;57this.uri = uri;58this.version = version;59this.languageId = alternativeFormat === 'text' ? getDefaultLanguage(doc) || 'python' : alternativeFormat;60}6162getText(range?: Range): string {63return this._alternativeDocument.getText(range);64}6566getSelection() {67return new Selection(0, 0, this.lineCount, 0);68}6970getWholeRange(): Range {71return new Range(0, 0, this.lineCount, 0);72}7374get lines(): string[] {75return this._alternativeDocument.lines;76}7778get lineCount(): number {79return this._alternativeDocument.lineCount;80}8182lineAt(line: number): TextLine;83lineAt(position: Position): TextLine;84lineAt(lineOrPosition: number | Position): TextLine {85let line: number | undefined;86if (lineOrPosition instanceof Position) {87line = lineOrPosition.line;88} else if (typeof lineOrPosition === 'number') {89line = lineOrPosition;90} else {91throw new Error(`Invalid argument`);92}93if (line < 0 || line >= this.lines.length) {94throw new Error('Illegal value for `line`');95}9697return this._alternativeDocument.lineAt(line);98}99offsetAt(position: Position): number {100return this._alternativeDocument.offsetAt(position);101}102positionAt(offset: number): Position {103return this._alternativeDocument.positionAt(offset);104}105validateRange(range: Range): Range {106return this._alternativeDocument.validateRange(range);107}108109validatePosition(position: Position): Position {110return this._alternativeDocument.validatePosition(position);111}112toJSON(): INotebookDocumentSnapshotJSON {113return {114type: 'notebook',115uri: this.uri.toJSON(),116languageId: this.languageId,117version: this.version,118_text: this._alternativeDocument.getText(),119alternativeFormat: this.alternativeFormat120};121}122}123124125