Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add editor encoding api #177434

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/vs/platform/extensions/common/extensionsApiProposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ const _allApiProposals = {
editSessionIdentityProvider: {
proposal: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.editSessionIdentityProvider.d.ts',
},
editorEncoding: {
proposal: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.editorEncoding.d.ts',
},
editorHoverVerbosityLevel: {
proposal: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.editorHoverVerbosityLevel.d.ts',
},
Expand Down
48 changes: 48 additions & 0 deletions src/vs/workbench/api/browser/mainThreadEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { IEditorControl } from 'vs/workbench/common/editor';
import { getCodeEditor, ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { DirtyDiffContribution } from 'vs/workbench/contrib/scm/browser/dirtydiffDecorator';
import { toEditorWithEncodingSupport } from 'vs/workbench/browser/parts/editor/editorStatus';
import { EncodingMode } from 'vs/workbench/services/textfile/common/textfiles';

export interface IMainThreadEditorLocator {
getEditor(id: string): MainThreadTextEditor | undefined;
Expand Down Expand Up @@ -274,6 +276,52 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {

return Promise.resolve([]);
}

$tryGetEncoding(id: string): Promise<string | undefined> {
const editor = this._editorLocator.getEditor(id);

if (!editor) {
return Promise.reject(new Error('No such TextEditor'));
}

const editorPanes = this._editorService.visibleEditorPanes;
for (const editorPane of editorPanes) {
if (editor.matches(editorPane)) {
const encodingSupport = toEditorWithEncodingSupport(editorPane.input);
if (!encodingSupport) {
return Promise.reject(new Error('No file active at this time'));
}

const encoding = encodingSupport.getEncoding();

return Promise.resolve(encoding);
}
}

return Promise.reject(new Error('No text editor active at this time'));
}

$trySetEncoding(id: string, encoding: string, mode: EncodingMode): Promise<void> {
const editor = this._editorLocator.getEditor(id);

if (!editor) {
return Promise.reject(new Error('No such TextEditor'));
}

const editorPanes = this._editorService.visibleEditorPanes;
for (const editorPane of editorPanes) {
if (editor.matches(editorPane)) {
const encodingSupport = toEditorWithEncodingSupport(editorPane.input);
if (!encodingSupport) {
return Promise.reject(new Error('No file active at this time'));
}

return encodingSupport.setEncoding(encoding, mode);
}
}

return Promise.reject(new Error('No text editor active at this time'));
}
}

// --- commands
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
NewSymbolNameTriggerKind: extHostTypes.NewSymbolNameTriggerKind,
InlineEdit: extHostTypes.InlineEdit,
InlineEditTriggerKind: extHostTypes.InlineEditTriggerKind,
EncodingMode: extHostTypes.EncodingMode,
};
};
}
3 changes: 3 additions & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import * as search from 'vs/workbench/services/search/common/search';
import { ISaveProfileResult } from 'vs/workbench/services/userDataProfile/common/userDataProfile';
import type { TerminalShellExecutionCommandLineConfidence } from 'vscode';
import { AuthInfo, Credentials } from 'vs/platform/request/common/request';
import { EncodingMode } from 'vs/workbench/services/textfile/common/textfiles';

export interface IWorkspaceData extends IStaticWorkspaceData {
folders: { uri: UriComponents; name: string; index: number }[];
Expand Down Expand Up @@ -282,6 +283,8 @@ export interface MainThreadTextEditorsShape extends IDisposable {
$tryApplyEdits(id: string, modelVersionId: number, edits: ISingleEditOperation[], opts: IApplyEditsOptions): Promise<boolean>;
$tryInsertSnippet(id: string, modelVersionId: number, template: string, selections: readonly IRange[], opts: IUndoStopOptions): Promise<boolean>;
$getDiffInformation(id: string): Promise<IChange[]>;
$tryGetEncoding(id: string): Promise<string | undefined>;
$trySetEncoding(id: string, encoding: string, mode: EncodingMode): Promise<void>;
}

export interface MainThreadTreeViewsShape extends IDisposable {
Expand Down
10 changes: 9 additions & 1 deletion src/vs/workbench/api/common/extHostTextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { IRange } from 'vs/editor/common/core/range';
import { ISingleEditOperation } from 'vs/editor/common/core/editOperation';
import { IResolvedTextEditorConfiguration, ITextEditorConfigurationUpdate, MainThreadTextEditorsShape } from 'vs/workbench/api/common/extHost.protocol';
import * as TypeConverters from 'vs/workbench/api/common/extHostTypeConverters';
import { EndOfLine, Position, Range, Selection, SnippetString, TextEditorLineNumbersStyle, TextEditorRevealType } from 'vs/workbench/api/common/extHostTypes';
import { EncodingMode, EndOfLine, Position, Range, Selection, SnippetString, TextEditorLineNumbersStyle, TextEditorRevealType } from 'vs/workbench/api/common/extHostTypes';
import type * as vscode from 'vscode';
import { ILogService } from 'vs/platform/log/common/log';
import { Lazy } from 'vs/base/common/lazy';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import * as TextFiles from 'vs/workbench/services/textfile/common/textfiles';

export class TextEditorDecorationType {

Expand Down Expand Up @@ -566,6 +567,13 @@ export class ExtHostTextEditor {
},
hide() {
_proxy.$tryHideEditor(id);
},
getEncoding(): Promise<string | undefined> {
return _proxy.$tryGetEncoding(id);
},
setEncoding(encoding: string, mode: EncodingMode): Promise<void> {
const _mode: TextFiles.EncodingMode = mode === EncodingMode.Encode ? TextFiles.EncodingMode.Encode : TextFiles.EncodingMode.Decode;
return _proxy.$trySetEncoding(id, encoding, _mode);
}
});
}
Expand Down
8 changes: 8 additions & 0 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4638,3 +4638,11 @@ export enum InlineEditTriggerKind {
}

//#endregion

//#region
export enum EncodingMode {
Encode = 0,
Decode = 1
}

//#endregion
4 changes: 3 additions & 1 deletion src/vs/workbench/api/test/browser/extHostTextEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ suite('ExtHostTextEditorOptions', () => {
$trySetSelections: undefined!,
$tryApplyEdits: undefined!,
$tryInsertSnippet: undefined!,
$getDiffInformation: undefined!
$getDiffInformation: undefined!,
$tryGetEncoding: undefined!,
$trySetEncoding: undefined!
};
opts = new ExtHostTextEditorOptions(mockProxy, '1', {
tabSize: 4,
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/parts/editor/editorStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class SideBySideEditorLanguageSupport implements ILanguageSupport {
}
}

function toEditorWithEncodingSupport(input: EditorInput): IEncodingSupport | null {
export function toEditorWithEncodingSupport(input: EditorInput): IEncodingSupport | null {

// Untitled Text Editor
if (input instanceof UntitledTextEditorInput) {
Expand Down
35 changes: 35 additions & 0 deletions src/vscode-dts/vscode.proposed.editorEncoding.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

declare module 'vscode' {
// https://github.com/microsoft/vscode/issues/824

export enum EncodingMode {

/**
* Instructs the encoding support to encode the object with the provided encoding
*/
Encode = 0,

/**
* Instructs the encoding support to decode the object with the provided encoding
*/
Decode = 1
}

export interface TextEditor {
/**
* Get the text editor encoding.
*/
getEncoding(): Thenable<string | undefined>;

/**
* Set the text editor encoding.
* @param encoding
* @param mode
*/
setEncoding(encoding: string, mode: EncodingMode): Thenable<void>;
}
}