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

update the a11y terminal buffer dynamically #176413

Merged
merged 26 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AccessibleBufferContribution extends DisposableStore implements ITerminalC
private _accessibleBufferWidget: AccessibleBufferWidget | undefined;

constructor(
private readonly _instance: ITerminalInstance,
instance: ITerminalInstance,
processManager: ITerminalProcessManager,
widgetManager: TerminalWidgetManager,
@IInstantiationService private readonly _instantiationService: IInstantiationService
Expand All @@ -42,7 +42,7 @@ class AccessibleBufferContribution extends DisposableStore implements ITerminalC
}

xtermReady(xterm: IXtermTerminal & { raw: Terminal }): void {
this._accessibleBufferWidget = this._instantiationService.createInstance(AccessibleBufferWidget, xterm, this._instance.capabilities);
this._accessibleBufferWidget = this._instantiationService.createInstance(AccessibleBufferWidget, xterm);
}
show(): void {
this._accessibleBufferWidget?.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
*--------------------------------------------------------------------------------------------*/

import { KeyCode } from 'vs/base/common/keyCodes';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { IEditorConstructionOptions } from 'vs/editor/browser/config/editorConfiguration';
import { EditorExtensionsRegistry } from 'vs/editor/browser/editorExtensions';
import { CodeEditorWidget, ICodeEditorWidgetOptions } from 'vs/editor/browser/widget/codeEditorWidget';
import { StringBuilder } from 'vs/editor/common/core/stringBuilder';
import { ITextModel } from 'vs/editor/common/model';
import { IModelService } from 'vs/editor/common/services/model';
import { LinkDetector } from 'vs/editor/contrib/links/browser/links';
import { localize } from 'vs/nls';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ITerminalCapabilityStore, TerminalCapability } from 'vs/platform/terminal/common/capabilities/capabilities';
import { TerminalSettingId } from 'vs/platform/terminal/common/terminal';
import { SelectionClipboardContributionID } from 'vs/workbench/contrib/codeEditor/browser/selectionClipboard';
import { getSimpleEditorOptions } from 'vs/workbench/contrib/codeEditor/browser/simpleEditorOptions';
Expand All @@ -33,16 +31,12 @@ export class AccessibleBufferWidget extends DisposableStore {
private _accessibleBuffer: HTMLElement;
private _bufferEditor: CodeEditorWidget;
private _editorContainer: HTMLElement;
private _commandFinishedDisposable: IDisposable | undefined;
private _refreshSelection: boolean = true;
private _registered: boolean = false;
private _lastContentLength: number = 0;
private _font: ITerminalFont;
private _xtermElement: HTMLElement;

constructor(
private readonly _xterm: IXtermTerminal & { raw: Terminal },
private readonly _capabilities: ITerminalCapabilityStore,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IModelService private readonly _modelService: IModelService,
@IConfigurationService private readonly _configurationService: IConfigurationService
Expand Down Expand Up @@ -87,6 +81,11 @@ export class AccessibleBufferWidget extends DisposableStore {
this._font = _xterm.getFont();
}
}));
this.add(this._xterm.raw.onWriteParsed(async () => {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
if (this._accessibleBuffer.classList.contains('active')) {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
await this._refresh();
}
}));
}

private _hide(): void {
Expand All @@ -95,42 +94,42 @@ export class AccessibleBufferWidget extends DisposableStore {
this._xterm.raw.focus();
}

async show(): Promise<void> {
const commandDetection = this._capabilities.get(TerminalCapability.CommandDetection);
const fragment = !!commandDetection ? this._getShellIntegrationContent() : this._getAllContent();
const model = await this._getTextModel(URI.from({ scheme: AccessibleBufferConstants.Scheme, fragment }));
if (model) {
this._bufferEditor.setModel(model);
private async _updateContent(): Promise<ITextModel> {
const model = await this._getTextModel(URI.from({ scheme: AccessibleBufferConstants.Scheme, fragment: this._getContent() }));
if (!model) {
throw new Error('Could not create accessible buffer editor model');
}
this._bufferEditor.setModel(model);
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
return model;
}

private async _refresh(): Promise<void> {
const model = await this._updateContent();
const lineNumber = model.getLineCount() - 1;
this._bufferEditor.setSelection({ startLineNumber: lineNumber, startColumn: 1, endLineNumber: lineNumber, endColumn: 1 });
this._bufferEditor.setScrollTop(this._bufferEditor.getScrollHeight());
this._accessibleBuffer.replaceChildren(this._editorContainer);
this._bufferEditor.focus();
}

async show(): Promise<void> {
if (!this._registered) {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
this._bufferEditor.layout({ width: this._xtermElement.clientWidth, height: this._xtermElement.clientHeight });
this._bufferEditor.onKeyDown((e) => {
if (e.keyCode === KeyCode.Escape || e.keyCode === KeyCode.Tab) {
this._hide();
}
});
if (commandDetection) {
this._commandFinishedDisposable = commandDetection.onCommandFinished(() => this._refreshSelection = true);
this.add(this._commandFinishedDisposable);
}
this._registered = true;
this._registerListeners();
}
await this._refresh();
this._accessibleBuffer.tabIndex = -1;
this._accessibleBuffer.classList.add('active');
this._xtermElement.classList.add('hide');
if (this._lastContentLength !== fragment.length || this._refreshSelection) {
let lineNumber = 1;
const lineCount = model?.getLineCount();
if (lineCount && model) {
lineNumber = commandDetection ? lineCount - 1 : lineCount > 2 ? lineCount - 2 : 1;
}

private _registerListeners(): void {
this._bufferEditor.layout({ width: this._xtermElement.clientWidth, height: this._xtermElement.clientHeight });
this._bufferEditor.onKeyDown((e) => {
if (e.keyCode === KeyCode.Escape || e.keyCode === KeyCode.Tab) {
this._hide();
}
this._bufferEditor.setSelection({ startLineNumber: lineNumber, startColumn: 1, endLineNumber: lineNumber, endColumn: 1 });
this._bufferEditor.setScrollTop(this._bufferEditor.getScrollHeight());
this._refreshSelection = false;
this._lastContentLength = fragment.length;
}
this._accessibleBuffer.replaceChildren(this._editorContainer);
this._bufferEditor.focus();
});
this._registered = true;
}

private async _getTextModel(resource: URI): Promise<ITextModel | null> {
Expand All @@ -142,32 +141,15 @@ export class AccessibleBufferWidget extends DisposableStore {
return this._modelService.createModel(resource.fragment, null, resource, false);
}

private _getShellIntegrationContent(): string {
const commands = this._capabilities.get(TerminalCapability.CommandDetection)?.commands;
const sb = new StringBuilder(10000);
if (!commands?.length) {
return this._getAllContent();
}
for (const command of commands) {
sb.appendString(command.command.replace(new RegExp(' ', 'g'), '\xA0'));
if (command.exitCode !== 0) {
sb.appendString(` exited with code ${command.exitCode}`);
}
sb.appendString('\n');
sb.appendString(command.getOutput()?.replace(new RegExp(' ', 'g'), '\xA0') || '');
}
return sb.build();
}

private _getAllContent(): string {
private _getContent(startLine?: number): string {
const lines: string[] = [];
let currentLine: string = '';
const buffer = this._xterm?.raw.buffer.active;
if (!buffer) {
return '';
}
const end = buffer.length;
for (let i = 0; i < end; i++) {
for (let i = startLine ?? 0; i <= end; i++) {
const line = buffer.getLine(i);
if (!line) {
continue;
Expand Down