mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 13:00:58 +00:00
Revert "clipboard variable resolver works with string, not clipboard service, #76847"
This reverts commit 7b463651d9
.
This commit is contained in:
parent
8c1327af61
commit
1042d28f97
|
@ -439,7 +439,7 @@ export class SnippetSession {
|
|||
|
||||
snippet.resolveVariables(new CompositeSnippetVariableResolver([
|
||||
modelBasedVariableResolver,
|
||||
new ClipboardBasedVariableResolver(clipboardService && clipboardService.readText(), idx, indexedSelections.length),
|
||||
new ClipboardBasedVariableResolver(clipboardService, idx, indexedSelections.length),
|
||||
new SelectionBasedVariableResolver(model, selection),
|
||||
new CommentBasedVariableResolver(model),
|
||||
new TimeBasedVariableResolver,
|
||||
|
|
|
@ -10,6 +10,7 @@ import { Selection } from 'vs/editor/common/core/selection';
|
|||
import { VariableResolver, Variable, Text } from 'vs/editor/contrib/snippet/snippetParser';
|
||||
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
|
||||
import { getLeadingWhitespace, commonPrefixLength, isFalsyOrWhitespace, pad, endsWith } from 'vs/base/common/strings';
|
||||
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
|
||||
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
|
||||
import { isSingleFolderWorkspaceIdentifier, toWorkspaceIdentifier, WORKSPACE_EXTENSION } from 'vs/platform/workspaces/common/workspaces';
|
||||
|
||||
|
@ -161,7 +162,7 @@ export class ModelBasedVariableResolver implements VariableResolver {
|
|||
export class ClipboardBasedVariableResolver implements VariableResolver {
|
||||
|
||||
constructor(
|
||||
private readonly _clipboardText: string | undefined,
|
||||
private readonly _clipboardService: IClipboardService,
|
||||
private readonly _selectionIdx: number,
|
||||
private readonly _selectionCount: number
|
||||
) {
|
||||
|
@ -169,19 +170,20 @@ export class ClipboardBasedVariableResolver implements VariableResolver {
|
|||
}
|
||||
|
||||
resolve(variable: Variable): string | undefined {
|
||||
if (variable.name !== 'CLIPBOARD') {
|
||||
if (variable.name !== 'CLIPBOARD' || !this._clipboardService) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!this._clipboardText) {
|
||||
const text = this._clipboardService.readText();
|
||||
if (!text) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const lines = this._clipboardText.split(/\r\n|\n|\r/).filter(s => !isFalsyOrWhitespace(s));
|
||||
const lines = text.split(/\r\n|\n|\r/).filter(s => !isFalsyOrWhitespace(s));
|
||||
if (lines.length === this._selectionCount) {
|
||||
return lines[this._selectionIdx];
|
||||
} else {
|
||||
return this._clipboardText;
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import { Selection } from 'vs/editor/common/core/selection';
|
|||
import { SelectionBasedVariableResolver, CompositeSnippetVariableResolver, ModelBasedVariableResolver, ClipboardBasedVariableResolver, TimeBasedVariableResolver, WorkspaceBasedVariableResolver } from 'vs/editor/contrib/snippet/snippetVariables';
|
||||
import { SnippetParser, Variable, VariableResolver } from 'vs/editor/contrib/snippet/snippetParser';
|
||||
import { TextModel } from 'vs/editor/common/model/textModel';
|
||||
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
|
||||
import { Workspace, toWorkspaceFolders, IWorkspace, IWorkspaceContextService, toWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
|
||||
|
||||
suite('Snippet Variables Resolver', function () {
|
||||
|
@ -208,25 +209,63 @@ suite('Snippet Variables Resolver', function () {
|
|||
|
||||
test('Add variable to insert value from clipboard to a snippet #40153', function () {
|
||||
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver(undefined, 1, 0), 'CLIPBOARD', undefined);
|
||||
let readTextResult: string | null | undefined;
|
||||
const clipboardService = new class implements IClipboardService {
|
||||
_serviceBrand: any;
|
||||
readText(): any { return readTextResult; }
|
||||
_throw = () => { throw new Error(); };
|
||||
writeText = this._throw;
|
||||
readFindText = this._throw;
|
||||
writeFindText = this._throw;
|
||||
writeResources = this._throw;
|
||||
readResources = this._throw;
|
||||
hasResources = this._throw;
|
||||
};
|
||||
let resolver = new ClipboardBasedVariableResolver(clipboardService, 1, 0);
|
||||
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver(null!, 1, 0), 'CLIPBOARD', undefined);
|
||||
readTextResult = undefined;
|
||||
assertVariableResolve(resolver, 'CLIPBOARD', undefined);
|
||||
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver('', 1, 0), 'CLIPBOARD', undefined);
|
||||
readTextResult = null;
|
||||
assertVariableResolve(resolver, 'CLIPBOARD', undefined);
|
||||
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver('foo', 1, 0), 'CLIPBOARD', 'foo');
|
||||
readTextResult = '';
|
||||
assertVariableResolve(resolver, 'CLIPBOARD', undefined);
|
||||
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver('foo', 1, 0), 'foo', undefined);
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver('foo', 1, 0), 'cLIPBOARD', undefined);
|
||||
readTextResult = 'foo';
|
||||
assertVariableResolve(resolver, 'CLIPBOARD', 'foo');
|
||||
|
||||
assertVariableResolve(resolver, 'foo', undefined);
|
||||
assertVariableResolve(resolver, 'cLIPBOARD', undefined);
|
||||
});
|
||||
|
||||
test('Add variable to insert value from clipboard to a snippet #40153', function () {
|
||||
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver('line1', 1, 2), 'CLIPBOARD', 'line1');
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver('line1\nline2\nline3', 1, 2), 'CLIPBOARD', 'line1\nline2\nline3');
|
||||
let readTextResult: string;
|
||||
let resolver: VariableResolver;
|
||||
const clipboardService = new class implements IClipboardService {
|
||||
_serviceBrand: any;
|
||||
readText(): string { return readTextResult; }
|
||||
_throw = () => { throw new Error(); };
|
||||
writeText = this._throw;
|
||||
readFindText = this._throw;
|
||||
writeFindText = this._throw;
|
||||
writeResources = this._throw;
|
||||
readResources = this._throw;
|
||||
hasResources = this._throw;
|
||||
};
|
||||
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver('line1\nline2', 1, 2), 'CLIPBOARD', 'line2');
|
||||
assertVariableResolve(new ClipboardBasedVariableResolver('line1\nline2', 0, 2), 'CLIPBOARD', 'line1');
|
||||
resolver = new ClipboardBasedVariableResolver(clipboardService, 1, 2);
|
||||
readTextResult = 'line1';
|
||||
assertVariableResolve(resolver, 'CLIPBOARD', 'line1');
|
||||
readTextResult = 'line1\nline2\nline3';
|
||||
assertVariableResolve(resolver, 'CLIPBOARD', 'line1\nline2\nline3');
|
||||
|
||||
readTextResult = 'line1\nline2';
|
||||
assertVariableResolve(resolver, 'CLIPBOARD', 'line2');
|
||||
readTextResult = 'line1\nline2';
|
||||
resolver = new ClipboardBasedVariableResolver(clipboardService, 0, 2);
|
||||
assertVariableResolve(resolver, 'CLIPBOARD', 'line1');
|
||||
});
|
||||
|
||||
|
||||
|
@ -297,4 +336,4 @@ suite('Snippet Variables Resolver', function () {
|
|||
workspace = new Workspace('', toWorkspaceFolders([{ path: 'folderName' }], workspaceConfigPath), workspaceConfigPath);
|
||||
assertVariableResolve(resolver, 'WORKSPACE_NAME', 'testWorkspace');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue