Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/editing/common/notebookDocumentSnapshot.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { NotebookDocument, TextLine, Uri } from 'vscode';
7
import { isNumber, isString } from '../../../util/vs/base/common/types';
8
import { isUriComponents, UriComponents } from '../../../util/vs/base/common/uri';
9
import { Position, Range, Selection } from '../../../vscodeTypes';
10
import { getAlternativeNotebookDocumentProvider } from '../../notebook/common/alternativeContent';
11
import { AlternativeNotebookDocument } from '../../notebook/common/alternativeNotebookDocument';
12
import { getDefaultLanguage } from '../../notebook/common/helpers';
13
14
export interface INotebookDocumentSnapshotJSON {
15
readonly type: 'notebook';
16
readonly uri: UriComponents;
17
readonly _text: string;
18
readonly languageId: string;
19
readonly version: number;
20
readonly alternativeFormat: 'json' | 'xml' | 'text';
21
}
22
23
export function isNotebookDocumentSnapshotJSON(thing: any): thing is INotebookDocumentSnapshotJSON {
24
if (!thing || typeof thing !== 'object') {
25
return false;
26
}
27
return thing.type === 'notebook' && isUriComponents(thing.uri) && isString(thing._text) &&
28
isString(thing.languageId) && isNumber(thing.version) && isString(thing.alternativeFormat);
29
}
30
31
export class NotebookDocumentSnapshot {
32
static create(doc: NotebookDocument, format: 'json' | 'xml' | 'text'): NotebookDocumentSnapshot {
33
const uri = doc.uri;
34
const version = doc.version;
35
36
const alternativeDocument = getAlternativeNotebookDocumentProvider(format).getAlternativeDocument(doc);
37
return new NotebookDocumentSnapshot(doc, uri, version, format, alternativeDocument);
38
}
39
static fromNewText(text: string, doc: NotebookDocumentSnapshot) {
40
const alternativeDocument = getAlternativeNotebookDocumentProvider(doc.alternativeFormat).getAlternativeDocumentFromText(text, doc.document);
41
const nd = new NotebookDocumentSnapshot(doc.document, doc.uri, doc.version, doc.alternativeFormat, alternativeDocument);
42
return nd;
43
}
44
static fromJSON(doc: NotebookDocument, json: INotebookDocumentSnapshotJSON): NotebookDocumentSnapshot {
45
// TODO@DonJayamanne
46
return NotebookDocumentSnapshot.create(doc, json.alternativeFormat);
47
}
48
49
readonly type = 'notebook';
50
readonly document: NotebookDocument;
51
readonly uri: Uri;
52
readonly version: number;
53
readonly languageId: string;
54
55
56
private constructor(doc: NotebookDocument, uri: Uri, version: number, public readonly alternativeFormat: 'json' | 'xml' | 'text', private readonly _alternativeDocument: AlternativeNotebookDocument) {
57
this.document = doc;
58
this.uri = uri;
59
this.version = version;
60
this.languageId = alternativeFormat === 'text' ? getDefaultLanguage(doc) || 'python' : alternativeFormat;
61
}
62
63
getText(range?: Range): string {
64
return this._alternativeDocument.getText(range);
65
}
66
67
getSelection() {
68
return new Selection(0, 0, this.lineCount, 0);
69
}
70
71
getWholeRange(): Range {
72
return new Range(0, 0, this.lineCount, 0);
73
}
74
75
get lines(): string[] {
76
return this._alternativeDocument.lines;
77
}
78
79
get lineCount(): number {
80
return this._alternativeDocument.lineCount;
81
}
82
83
lineAt(line: number): TextLine;
84
lineAt(position: Position): TextLine;
85
lineAt(lineOrPosition: number | Position): TextLine {
86
let line: number | undefined;
87
if (lineOrPosition instanceof Position) {
88
line = lineOrPosition.line;
89
} else if (typeof lineOrPosition === 'number') {
90
line = lineOrPosition;
91
} else {
92
throw new Error(`Invalid argument`);
93
}
94
if (line < 0 || line >= this.lines.length) {
95
throw new Error('Illegal value for `line`');
96
}
97
98
return this._alternativeDocument.lineAt(line);
99
}
100
offsetAt(position: Position): number {
101
return this._alternativeDocument.offsetAt(position);
102
}
103
positionAt(offset: number): Position {
104
return this._alternativeDocument.positionAt(offset);
105
}
106
validateRange(range: Range): Range {
107
return this._alternativeDocument.validateRange(range);
108
}
109
110
validatePosition(position: Position): Position {
111
return this._alternativeDocument.validatePosition(position);
112
}
113
toJSON(): INotebookDocumentSnapshotJSON {
114
return {
115
type: 'notebook',
116
uri: this.uri.toJSON(),
117
languageId: this.languageId,
118
version: this.version,
119
_text: this._alternativeDocument.getText(),
120
alternativeFormat: this.alternativeFormat
121
};
122
}
123
}
124
125