Skip to content

Commit

Permalink
Revert "Fixes #224943" (#225161)
Browse files Browse the repository at this point in the history
This reverts commit adbf397.
  • Loading branch information
hediet authored Aug 9, 2024
1 parent 058a32e commit 4693ac3
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions src/vs/editor/common/services/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ILanguageNameIdPair, ILanguageSelection, ILanguageService, ILanguageIco
import { firstOrDefault } from 'vs/base/common/arrays';
import { ILanguageIdCodec, TokenizationRegistry } from 'vs/editor/common/languages';
import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry';
import { IObservable, observableFromEvent } from 'vs/base/common/observable';

export class LanguageService extends Disposable implements ILanguageService {
public _serviceBrand: undefined;
Expand Down Expand Up @@ -151,13 +150,51 @@ export class LanguageService extends Disposable implements ILanguageService {
}

class LanguageSelection implements ILanguageSelection {
private readonly _value: IObservable<string>;
public readonly onDidChange: Event<string>;

constructor(onDidChangeLanguages: Event<void>, selector: () => string) {
this._value = observableFromEvent(this, onDidChangeLanguages, () => selector());
this.onDidChange = Event.fromObservable(this._value);
public languageId: string;

private _listener: IDisposable | null = null;
private _emitter: Emitter<string> | null = null;

constructor(
private readonly _onDidChangeLanguages: Event<void>,
private readonly _selector: () => string
) {
this.languageId = this._selector();
}

private _dispose(): void {
if (this._listener) {
this._listener.dispose();
this._listener = null;
}
if (this._emitter) {
this._emitter.dispose();
this._emitter = null;
}
}

public get languageId(): string { return this._value.get(); }
public get onDidChange(): Event<string> {
if (!this._listener) {
this._listener = this._onDidChangeLanguages(() => this._evaluate());
}
if (!this._emitter) {
this._emitter = new Emitter<string>({
onDidRemoveLastListener: () => {
this._dispose();
}
});
}
return this._emitter.event;
}

private _evaluate(): void {
const languageId = this._selector();
if (languageId === this.languageId) {
// no change
return;
}
this.languageId = languageId;
this._emitter?.fire(this.languageId);
}
}

0 comments on commit 4693ac3

Please sign in to comment.