@reactive-vscode/mock 0.2.6 → 0.2.7-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reactive-vscode/mock",
3
3
  "type": "module",
4
- "version": "0.2.6",
4
+ "version": "0.2.7-beta.1",
5
5
  "description": "Mock VSCode API for testing",
6
6
  "author": "_Kerman <kermanx@qq.com>",
7
7
  "license": "MIT",
@@ -1,73 +1,73 @@
1
- import type vscode from 'vscode'
2
- import type { MockVscode } from '../ns'
3
- import { resolve } from 'node:path'
4
- import { vi } from 'vitest'
5
- import { ExtensionKind } from 'vscode'
6
- import { Uri } from '../class/Uri'
7
- import { ExtensionMode } from '../enum/ExtensionMode'
8
- import { Unimplemented } from '../utils/unimplemented'
9
- import { Memento } from './Memento'
10
- import { SecretStorage } from './SecretStorage'
11
-
12
- export function createExtensionContext(_context: MockVscode) {
13
- const extension: vscode.Extension<any> = {
14
- id: _context._extention.identifier,
15
- extensionUri: Uri.file(_context._extention.root),
16
- extensionPath: _context._extention.root,
17
- isActive: true,
18
- packageJSON: _context._extention,
19
- extensionKind: ExtensionKind.Workspace,
20
- exports: Unimplemented('TODO'),
21
- activate: vi.fn(),
22
- }
23
- return new (class implements vscode.ExtensionContext {
24
- _subscriptions: { dispose: () => any }[] = []
25
- get subscriptions() {
26
- return this._subscriptions
27
- }
28
-
29
- _workspaceState = {}
30
- get workspaceState() {
31
- return new Memento(() => this._workspaceState)
32
- }
33
-
34
- _globalState = {}
35
- get globalState() {
36
- return new Memento(() => this._globalState)
37
- }
38
-
39
- secrets = new SecretStorage(_context._config.init.extension.secrets)
40
-
41
- extensionUri = extension.extensionUri
42
-
43
- extensionPath = extension.extensionPath
44
-
45
- environmentVariableCollection = Unimplemented('TODO')
46
-
47
- asAbsolutePath(relativePath: string): string {
48
- return resolve(_context._extention.root, relativePath)
49
- }
50
-
51
- storageUri = Unimplemented('TODO')
52
-
53
- storagePath = Unimplemented('TODO')
54
-
55
- globalStorageUri = Unimplemented('TODO')
56
-
57
- globalStoragePath = Unimplemented('TODO')
58
-
59
- logUri = Unimplemented('TODO')
60
-
61
- logPath = Unimplemented('TODO')
62
-
63
- extensionMode = ExtensionMode.Test
64
-
65
- extension = extension
66
- })()
67
- }
68
-
69
- export type ExtensionContext = ReturnType<typeof createExtensionContext>
70
-
71
- export const defaultExtensionInitConfig = {
72
- secrets: {},
73
- }
1
+ import type vscode from 'vscode'
2
+ import type { MockVscode } from '../ns'
3
+ import { resolve } from 'node:path'
4
+ import { vi } from 'vitest'
5
+ import { ExtensionKind } from 'vscode'
6
+ import { Uri } from '../class/Uri'
7
+ import { ExtensionMode } from '../enum/ExtensionMode'
8
+ import { Unimplemented } from '../utils/unimplemented'
9
+ import { Memento } from './Memento'
10
+ import { SecretStorage } from './SecretStorage'
11
+
12
+ export function createExtensionContext(_context: MockVscode) {
13
+ const extension: vscode.Extension<any> = {
14
+ id: _context._extention.identifier,
15
+ extensionUri: Uri.file(_context._extention.root),
16
+ extensionPath: _context._extention.root,
17
+ isActive: true,
18
+ packageJSON: _context._extention,
19
+ extensionKind: ExtensionKind.Workspace,
20
+ exports: Unimplemented('TODO'),
21
+ activate: vi.fn(),
22
+ }
23
+ return new (class implements vscode.ExtensionContext {
24
+ _subscriptions: { dispose: () => any }[] = []
25
+ get subscriptions() {
26
+ return this._subscriptions
27
+ }
28
+
29
+ _workspaceState = {}
30
+ get workspaceState() {
31
+ return new Memento(() => this._workspaceState)
32
+ }
33
+
34
+ _globalState = {}
35
+ get globalState() {
36
+ return new Memento(() => this._globalState)
37
+ }
38
+
39
+ secrets = new SecretStorage(_context._config.init.extension.secrets)
40
+
41
+ extensionUri = extension.extensionUri
42
+
43
+ extensionPath = extension.extensionPath
44
+
45
+ environmentVariableCollection = Unimplemented('TODO')
46
+
47
+ asAbsolutePath(relativePath: string): string {
48
+ return resolve(_context._extention.root, relativePath)
49
+ }
50
+
51
+ storageUri = Unimplemented('TODO')
52
+
53
+ storagePath = Unimplemented('TODO')
54
+
55
+ globalStorageUri = Unimplemented('TODO')
56
+
57
+ globalStoragePath = Unimplemented('TODO')
58
+
59
+ logUri = Unimplemented('TODO')
60
+
61
+ logPath = Unimplemented('TODO')
62
+
63
+ extensionMode = ExtensionMode.Test
64
+
65
+ extension = extension
66
+ })()
67
+ }
68
+
69
+ export type ExtensionContext = ReturnType<typeof createExtensionContext>
70
+
71
+ export const defaultExtensionInitConfig = {
72
+ secrets: {},
73
+ }
@@ -1,21 +1,21 @@
1
- import type vscode from 'vscode'
2
- import { vi } from 'vitest'
3
-
4
- export class Memento implements vscode.Memento {
5
- constructor(public _getData: () => any) {}
6
-
7
- keys(): readonly string[] {
8
- return Object.keys(this._getData())
9
- }
10
-
11
- get<T>(key: string, defaultValue?: T): T | undefined {
12
- return this._getData()[key] ?? defaultValue
13
- }
14
-
15
- async _update(key: string, value: any): Promise<void> {
16
- this._getData()[key] = value
17
- }
18
- update = vi.fn(this._update)
19
-
20
- setKeysForSync = vi.fn()
21
- }
1
+ import type vscode from 'vscode'
2
+ import { vi } from 'vitest'
3
+
4
+ export class Memento implements vscode.Memento {
5
+ constructor(public _getData: () => any) {}
6
+
7
+ keys(): readonly string[] {
8
+ return Object.keys(this._getData())
9
+ }
10
+
11
+ get<T>(key: string, defaultValue?: T): T | undefined {
12
+ return this._getData()[key] ?? defaultValue
13
+ }
14
+
15
+ async _update(key: string, value: any): Promise<void> {
16
+ this._getData()[key] = value
17
+ }
18
+ update = vi.fn(this._update)
19
+
20
+ setKeysForSync = vi.fn()
21
+ }
@@ -1,147 +1,147 @@
1
- import type vscode from 'vscode'
2
- import type { MockVscode } from '../ns/index'
3
- import { Position } from '../class/Position'
4
- import { Range } from '../class/Range'
5
- import { EndOfLine } from '../enum/EndOfLine'
6
- import { Unimplemented } from '../utils/unimplemented'
7
-
8
- export class TextDocument implements vscode.TextDocument {
9
- constructor(
10
- public _context: MockVscode,
11
- public uri: vscode.Uri,
12
- public languageId: string,
13
- public eol: EndOfLine,
14
- ) {
15
- }
16
-
17
- _change() {
18
- this._version++
19
- this._context.workspace._onDidChangeTextDocument.fire({
20
- document: this,
21
- contentChanges: [Unimplemented('context changes')],
22
- reason: undefined,
23
- })
24
- }
25
-
26
- _setText(text: string) {
27
- this._dirty = true
28
- this._text = text
29
- this._change()
30
- }
31
-
32
- _close() {
33
- this._context.workspace._closeTextDocument(this)
34
- }
35
-
36
- _dirty = false
37
- _version = 0
38
- _closed = false
39
- _text = ''
40
-
41
- get fileName(): string {
42
- return this.uri.fsPath
43
- }
44
-
45
- get isUntitled(): boolean {
46
- return false
47
- }
48
-
49
- get isDirty(): boolean {
50
- return this._dirty
51
- }
52
-
53
- get version(): number {
54
- return this._version
55
- }
56
-
57
- get isClosed(): boolean {
58
- return this._closed
59
- }
60
-
61
- async save(): Promise<boolean> {
62
- return !!await this._context.workspace.save(this.uri)
63
- }
64
-
65
- get _lines(): string[] {
66
- return this.eol === EndOfLine.LF ? this._text.split('\n') : this._text.split('\r\n')
67
- }
68
-
69
- get lineCount(): number {
70
- return this._lines.length
71
- }
72
-
73
- lineAt(lineOrPos: number | Position): vscode.TextLine {
74
- const line = typeof lineOrPos === 'number' ? lineOrPos : lineOrPos.line
75
- const text = this._lines[line]
76
- return {
77
- lineNumber: line,
78
- text,
79
- range: new Range(line, 0, line, text.length),
80
- rangeIncludingLineBreak: new Range(line, 0, line + 1, 0),
81
- firstNonWhitespaceCharacterIndex: text.search(/\S|$/),
82
- isEmptyOrWhitespace: !text.trim(),
83
- }
84
- }
85
-
86
- offsetAt(position: Position): number {
87
- let offset = 0
88
- for (let i = 0; i < position.line; i++) {
89
- offset += this._lines[i].length + this.eol
90
- }
91
- return offset + position.character
92
- }
93
-
94
- positionAt(offset: number): Position {
95
- let line = 0
96
- while (line < this.lineCount && offset >= this._lines[line].length) {
97
- offset -= this._lines[line].length + this.eol
98
- line++
99
- }
100
- return new Position(line, offset)
101
- }
102
-
103
- getText(range?: Range): string {
104
- if (!range) {
105
- return this._text
106
- }
107
- const start = this.offsetAt(range.start)
108
- const end = this.offsetAt(range.end)
109
- return this._text.substring(start, end)
110
- }
111
-
112
- getWordRangeAtPosition(_position: Position, _regex?: RegExp): Range | undefined {
113
- return Unimplemented('WordRangeAtPosition')
114
- }
115
-
116
- validateRange(range: Range): Range {
117
- return new Range(
118
- this.validatePosition(range.start),
119
- this.validatePosition(range.end),
120
- )
121
- }
122
-
123
- validatePosition(position: Position): Position {
124
- return position.line < this.lineCount
125
- ? position.character < this._lines[position.line].length
126
- ? position
127
- : new Position(position.line, this._lines[position.line].length)
128
- : this.positionAt(this._text.length)
129
- }
130
-
131
- _applyEdit(edit: vscode.TextEdit) {
132
- const start = this.offsetAt(edit.range.start)
133
- const end = this.offsetAt(edit.range.end)
134
- this._text = this._text.slice(0, start) + edit.newText + this._text.slice(end)
135
- if (edit.newEol)
136
- this._changeEol(edit.newEol)
137
- }
138
-
139
- _changeEol(newEol: vscode.EndOfLine) {
140
- if (this.eol === newEol)
141
- return
142
- if (newEol === EndOfLine.LF)
143
- this._text = this._text.replaceAll('\r\n', '\n')
144
- else
145
- this._text = this._text.replaceAll(/\r?\n/g, '\r\n')
146
- }
147
- }
1
+ import type vscode from 'vscode'
2
+ import type { MockVscode } from '../ns/index'
3
+ import { Position } from '../class/Position'
4
+ import { Range } from '../class/Range'
5
+ import { EndOfLine } from '../enum/EndOfLine'
6
+ import { Unimplemented } from '../utils/unimplemented'
7
+
8
+ export class TextDocument implements vscode.TextDocument {
9
+ constructor(
10
+ public _context: MockVscode,
11
+ public uri: vscode.Uri,
12
+ public languageId: string,
13
+ public eol: EndOfLine,
14
+ ) {
15
+ }
16
+
17
+ _change() {
18
+ this._version++
19
+ this._context.workspace._onDidChangeTextDocument.fire({
20
+ document: this,
21
+ contentChanges: [Unimplemented('context changes')],
22
+ reason: undefined,
23
+ })
24
+ }
25
+
26
+ _setText(text: string) {
27
+ this._dirty = true
28
+ this._text = text
29
+ this._change()
30
+ }
31
+
32
+ _close() {
33
+ this._context.workspace._closeTextDocument(this)
34
+ }
35
+
36
+ _dirty = false
37
+ _version = 0
38
+ _closed = false
39
+ _text = ''
40
+
41
+ get fileName(): string {
42
+ return this.uri.fsPath
43
+ }
44
+
45
+ get isUntitled(): boolean {
46
+ return false
47
+ }
48
+
49
+ get isDirty(): boolean {
50
+ return this._dirty
51
+ }
52
+
53
+ get version(): number {
54
+ return this._version
55
+ }
56
+
57
+ get isClosed(): boolean {
58
+ return this._closed
59
+ }
60
+
61
+ async save(): Promise<boolean> {
62
+ return !!await this._context.workspace.save(this.uri)
63
+ }
64
+
65
+ get _lines(): string[] {
66
+ return this.eol === EndOfLine.LF ? this._text.split('\n') : this._text.split('\r\n')
67
+ }
68
+
69
+ get lineCount(): number {
70
+ return this._lines.length
71
+ }
72
+
73
+ lineAt(lineOrPos: number | Position): vscode.TextLine {
74
+ const line = typeof lineOrPos === 'number' ? lineOrPos : lineOrPos.line
75
+ const text = this._lines[line]
76
+ return {
77
+ lineNumber: line,
78
+ text,
79
+ range: new Range(line, 0, line, text.length),
80
+ rangeIncludingLineBreak: new Range(line, 0, line + 1, 0),
81
+ firstNonWhitespaceCharacterIndex: text.search(/\S|$/),
82
+ isEmptyOrWhitespace: !text.trim(),
83
+ }
84
+ }
85
+
86
+ offsetAt(position: Position): number {
87
+ let offset = 0
88
+ for (let i = 0; i < position.line; i++) {
89
+ offset += this._lines[i].length + this.eol
90
+ }
91
+ return offset + position.character
92
+ }
93
+
94
+ positionAt(offset: number): Position {
95
+ let line = 0
96
+ while (line < this.lineCount && offset >= this._lines[line].length) {
97
+ offset -= this._lines[line].length + this.eol
98
+ line++
99
+ }
100
+ return new Position(line, offset)
101
+ }
102
+
103
+ getText(range?: Range): string {
104
+ if (!range) {
105
+ return this._text
106
+ }
107
+ const start = this.offsetAt(range.start)
108
+ const end = this.offsetAt(range.end)
109
+ return this._text.substring(start, end)
110
+ }
111
+
112
+ getWordRangeAtPosition(_position: Position, _regex?: RegExp): Range | undefined {
113
+ return Unimplemented('WordRangeAtPosition')
114
+ }
115
+
116
+ validateRange(range: Range): Range {
117
+ return new Range(
118
+ this.validatePosition(range.start),
119
+ this.validatePosition(range.end),
120
+ )
121
+ }
122
+
123
+ validatePosition(position: Position): Position {
124
+ return position.line < this.lineCount
125
+ ? position.character < this._lines[position.line].length
126
+ ? position
127
+ : new Position(position.line, this._lines[position.line].length)
128
+ : this.positionAt(this._text.length)
129
+ }
130
+
131
+ _applyEdit(edit: vscode.TextEdit) {
132
+ const start = this.offsetAt(edit.range.start)
133
+ const end = this.offsetAt(edit.range.end)
134
+ this._text = this._text.slice(0, start) + edit.newText + this._text.slice(end)
135
+ if (edit.newEol)
136
+ this._changeEol(edit.newEol)
137
+ }
138
+
139
+ _changeEol(newEol: vscode.EndOfLine) {
140
+ if (this.eol === newEol)
141
+ return
142
+ if (newEol === EndOfLine.LF)
143
+ this._text = this._text.replaceAll('\r\n', '\n')
144
+ else
145
+ this._text = this._text.replaceAll(/\r?\n/g, '\r\n')
146
+ }
147
+ }
@@ -1,78 +1,78 @@
1
- import type vscode from 'vscode'
2
- import type { MockVscode } from '../ns/index'
3
- import type { TextDocument } from './TextDocument'
4
- import { vi } from 'vitest'
5
- import { TextEditorEdit } from './TextEditorEdit'
6
-
7
- export class TextEditor implements vscode.TextEditor {
8
- constructor(
9
- public _context: MockVscode,
10
- public _document: TextDocument,
11
- ) {
12
- }
13
-
14
- _selections: vscode.Selection[] = []
15
- _viewColumn: vscode.ViewColumn | undefined
16
- _visibleRanges: vscode.Range[] = []
17
-
18
- _updateVisibleRanges(val: vscode.Range[]) {
19
- this._visibleRanges = val
20
- this._context.window._onDidChangeTextEditorVisibleRanges.fire({
21
- textEditor: this,
22
- visibleRanges: this._visibleRanges,
23
- })
24
- }
25
-
26
- get document(): vscode.TextDocument {
27
- return this._document
28
- }
29
-
30
- get selection(): vscode.Selection {
31
- return this._selections[0]
32
- }
33
-
34
- get selections(): readonly vscode.Selection[] {
35
- return this._selections
36
- }
37
-
38
- options: vscode.TextEditorOptions = {}
39
-
40
- get viewColumn(): vscode.ViewColumn | undefined {
41
- return this._viewColumn
42
- }
43
-
44
- get visibleRanges(): vscode.Range[] {
45
- return this._visibleRanges
46
- }
47
-
48
- edit = vi.fn(async (callback: (editBuilder: vscode.TextEditorEdit) => void, _options?: any): Promise<boolean> => {
49
- callback(new TextEditorEdit(this._document))
50
- return true
51
- })
52
-
53
- insertSnippet = vi.fn(async (): Promise<boolean> => {
54
- this._document._change()
55
- return true
56
- })
57
-
58
- _decorations = new Map<vscode.TextEditorDecorationType, vscode.Range[] | vscode.DecorationOptions[]>()
59
-
60
- setDecorations = vi.fn((decorationType: vscode.TextEditorDecorationType, rangesOrOptions: vscode.Range[] | vscode.DecorationOptions[]): void => {
61
- this._decorations.set(
62
- decorationType,
63
- rangesOrOptions,
64
- )
65
- })
66
-
67
- revealRange = vi.fn((range: vscode.Range, _revealType?: vscode.TextEditorRevealType) => {
68
- this._updateVisibleRanges([range])
69
- })
70
-
71
- show = vi.fn(() => {
72
- throw new Error('Deprecated')
73
- })
74
-
75
- hide = vi.fn(() => {
76
- throw new Error('Deprecated')
77
- })
78
- }
1
+ import type vscode from 'vscode'
2
+ import type { MockVscode } from '../ns/index'
3
+ import type { TextDocument } from './TextDocument'
4
+ import { vi } from 'vitest'
5
+ import { TextEditorEdit } from './TextEditorEdit'
6
+
7
+ export class TextEditor implements vscode.TextEditor {
8
+ constructor(
9
+ public _context: MockVscode,
10
+ public _document: TextDocument,
11
+ ) {
12
+ }
13
+
14
+ _selections: vscode.Selection[] = []
15
+ _viewColumn: vscode.ViewColumn | undefined
16
+ _visibleRanges: vscode.Range[] = []
17
+
18
+ _updateVisibleRanges(val: vscode.Range[]) {
19
+ this._visibleRanges = val
20
+ this._context.window._onDidChangeTextEditorVisibleRanges.fire({
21
+ textEditor: this,
22
+ visibleRanges: this._visibleRanges,
23
+ })
24
+ }
25
+
26
+ get document(): vscode.TextDocument {
27
+ return this._document
28
+ }
29
+
30
+ get selection(): vscode.Selection {
31
+ return this._selections[0]
32
+ }
33
+
34
+ get selections(): readonly vscode.Selection[] {
35
+ return this._selections
36
+ }
37
+
38
+ options: vscode.TextEditorOptions = {}
39
+
40
+ get viewColumn(): vscode.ViewColumn | undefined {
41
+ return this._viewColumn
42
+ }
43
+
44
+ get visibleRanges(): vscode.Range[] {
45
+ return this._visibleRanges
46
+ }
47
+
48
+ edit = vi.fn(async (callback: (editBuilder: vscode.TextEditorEdit) => void, _options?: any): Promise<boolean> => {
49
+ callback(new TextEditorEdit(this._document))
50
+ return true
51
+ })
52
+
53
+ insertSnippet = vi.fn(async (): Promise<boolean> => {
54
+ this._document._change()
55
+ return true
56
+ })
57
+
58
+ _decorations = new Map<vscode.TextEditorDecorationType, vscode.Range[] | vscode.DecorationOptions[]>()
59
+
60
+ setDecorations = vi.fn((decorationType: vscode.TextEditorDecorationType, rangesOrOptions: vscode.Range[] | vscode.DecorationOptions[]): void => {
61
+ this._decorations.set(
62
+ decorationType,
63
+ rangesOrOptions,
64
+ )
65
+ })
66
+
67
+ revealRange = vi.fn((range: vscode.Range, _revealType?: vscode.TextEditorRevealType) => {
68
+ this._updateVisibleRanges([range])
69
+ })
70
+
71
+ show = vi.fn(() => {
72
+ throw new Error('Deprecated')
73
+ })
74
+
75
+ hide = vi.fn(() => {
76
+ throw new Error('Deprecated')
77
+ })
78
+ }