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

Fix: wrong init call on set filename #149

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
68 changes: 41 additions & 27 deletions packages/ace-linters/src/language-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
mergeTokens,
parseSemanticTokens
} from "./type-converters/lsp/semantic-tokens";
import {URI} from "vscode-uri";
import {LightbulbWidget} from "./components/lightbulb";
import {AceVirtualRenderer} from "./ace/renderer-singleton";
import {AceEditor} from "./ace/editor-singleton";
Expand All @@ -64,8 +63,6 @@ export class LanguageProvider {
this.$signatureTooltip = new SignatureTooltip(this);
}



/**
* Creates LanguageProvider using our transport protocol with ability to register different services on same
* webworker
Expand Down Expand Up @@ -519,11 +516,11 @@ class SessionLanguageProvider {
private $messageController: IMessageController;
private $deltaQueue: Ace.Delta[] | null;
private $isConnected = false;
private $modeIsChanged = false;
private $options?: ServiceOptions;
private $filePath: string;
private $isFilePathRequired = false;
private $servicesCapabilities?: { [serviceName: string]: lsp.ServerCapabilities };
private $requestsQueue: Function[] = [];

state: {
occurrenceMarkers: MarkerGroup | null,
Expand Down Expand Up @@ -557,7 +554,7 @@ class SessionLanguageProvider {
this.editor = editor;
this.$isFilePathRequired = provider.requireFilePath;

session.doc["version"] = 1;
session.doc.version = 1;
session.doc.on("change", this.$changeListener, true);
this.addSemanticTokenSupport(session); //TODO: ?
session.on("changeMode", this.$changeMode);
Expand All @@ -568,6 +565,14 @@ class SessionLanguageProvider {
this.$init();
}

enqueueIfNotConnected(callback: () => void) {
if (!this.$isConnected) {
this.$requestsQueue.push(callback);
} else {
callback();
}
}

get comboDocumentIdentifier(): ComboDocumentIdentifier {
return {
documentUri: this.documentUri,
Expand All @@ -579,11 +584,16 @@ class SessionLanguageProvider {
* @param filePath
*/
setFilePath(filePath: string) {
if (this.$filePath !== undefined)//TODO change file path
return;
this.$filePath = filePath;
this.$init();
}
this.enqueueIfNotConnected(() => {
this.session.doc.version++;
if (this.$filePath !== undefined)//TODO change file path
return;
this.$filePath = filePath;
const previousComboId = this.comboDocumentIdentifier;
this.initDocumentUri(true);
this.$messageController.renameDocument(previousComboId, this.comboDocumentIdentifier.documentUri, this.session.doc.version);
})
};

private $init() {
if (this.$isFilePathRequired && this.$filePath === undefined)
Expand Down Expand Up @@ -627,29 +637,29 @@ class SessionLanguageProvider {
this.$isConnected = true;

this.setServerCapabilities(capabilities);
if (this.$modeIsChanged)
this.$changeMode();

this.$requestsQueue.forEach((requestCallback) => requestCallback());
this.$requestsQueue = [];

if (this.$deltaQueue)
this.$sendDeltaQueue();
if (this.$options)
this.setOptions(this.$options);
}

private $changeMode = () => {
if (!this.$isConnected) {
this.$modeIsChanged = true;
return;
}
this.$deltaQueue = [];
this.enqueueIfNotConnected(() => {
this.$deltaQueue = [];

this.session.clearAnnotations();
if (this.state.diagnosticMarkers) {
this.state.diagnosticMarkers.setMarkers([]);
}
this.session.clearAnnotations();
if (this.state.diagnosticMarkers) {
this.state.diagnosticMarkers.setMarkers([]);
}

this.session.setSemanticTokens(undefined); //clear all semantic tokens
let newVersion = this.session.doc["version"]++;
this.$messageController.changeMode(this.comboDocumentIdentifier, this.session.getValue(), newVersion, this.$mode, this.setServerCapabilities);
this.session.setSemanticTokens(undefined); //clear all semantic tokens
let newVersion = this.session.doc.version++;
this.$messageController.changeMode(this.comboDocumentIdentifier, this.session.getValue(), newVersion, this.$mode, this.setServerCapabilities);
});
};

setServerCapabilities = (capabilities: { [serviceName: string]: lsp.ServerCapabilities }) => {
Expand Down Expand Up @@ -688,8 +698,11 @@ class SessionLanguageProvider {
//or we shoudl use service with full format capability instead of range one's
}

private initDocumentUri() {
private initDocumentUri(isRename = false) {
let filePath = this.$filePath ?? this.session["id"] + "." + this.$extension;
if (isRename) {
delete this.$provider.$urisToSessionsIds[this.documentUri];
}
this.documentUri = convertToUri(filePath);
this.$provider.$urisToSessionsIds[this.documentUri] = this.session["id"];
}
Expand All @@ -711,7 +724,7 @@ class SessionLanguageProvider {
}

private $changeListener = (delta) => {
this.session.doc["version"]++;
this.session.doc.version++;
if (!this.$deltaQueue) {
this.$deltaQueue = [];
setTimeout(() => this.$sendDeltaQueue(() => {
Expand All @@ -734,8 +747,9 @@ class SessionLanguageProvider {
if (!diagnostics) {
return;
}

let annotations = toAnnotations(diagnostics);
this.session.clearAnnotations();
let annotations = toAnnotations(diagnostics)
if (annotations && annotations.length > 0) {
this.session.setAnnotations(annotations);
}
Expand Down
10 changes: 7 additions & 3 deletions packages/ace-linters/src/message-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
GetCodeActionsMessage,
ExecuteCommandMessage,
AppliedEditMessage,
SetWorkspaceMessage
SetWorkspaceMessage, RenameDocumentMessage
} from "./message-types";
import {ComboDocumentIdentifier, IMessageController} from "./types/message-controller-interface";
import * as lsp from "vscode-languageserver-protocol";
Expand Down Expand Up @@ -112,9 +112,9 @@ export class MessageController implements IMessageController {
change(documentIdentifier: ComboDocumentIdentifier, deltas: lsp.TextDocumentContentChangeEvent[], document: Ace.Document, callback?: () => void) {
let message: BaseMessage;
if (deltas.length > 50 && deltas.length > document.getLength() >> 1) {
message = new ChangeMessage(documentIdentifier, this.callbackId++, document.getValue(), document["version"]);
message = new ChangeMessage(documentIdentifier, this.callbackId++, document.getValue(), document.version);
} else {
message = new DeltasMessage(documentIdentifier, this.callbackId++, deltas, document["version"]);
message = new DeltasMessage(documentIdentifier, this.callbackId++, deltas, document.version);
}

this.postMessage(message, callback)
Expand Down Expand Up @@ -169,6 +169,10 @@ export class MessageController implements IMessageController {
this.$worker.postMessage(new SetWorkspaceMessage(workspaceUri));
}

renameDocument(documentIdentifier: ComboDocumentIdentifier, newDocumentUri: string, version: number): void {
this.$worker.postMessage(new RenameDocumentMessage(documentIdentifier, this.callbackId++, newDocumentUri, version));
}

postMessage(message: BaseMessage | CloseConnectionMessage | ExecuteCommandMessage, callback?: (any) => void) {
if (callback) {
this.callbacks[message.callbackId] = callback;
Expand Down
20 changes: 17 additions & 3 deletions packages/ace-linters/src/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export abstract class BaseMessage {
version?: number;
callbackId: number;

protected constructor(documentIdentifier: ComboDocumentIdentifier, callbackId: number) {
constructor(documentIdentifier: ComboDocumentIdentifier, callbackId: number) {
this.sessionId = documentIdentifier.sessionId;
this.documentUri = documentIdentifier.documentUri;
this.callbackId = callbackId;
Expand Down Expand Up @@ -255,6 +255,18 @@ export class AppliedEditMessage {
}
}

export class RenameDocumentMessage extends BaseMessage {
type: MessageType.renameDocument = MessageType.renameDocument;
value: string;
version: number;

constructor(documentIdentifier: ComboDocumentIdentifier, callbackId: number, value: string, version: number) {
super(documentIdentifier, callbackId);
this.value = value;
this.version = version;
}
}

export enum MessageType {
init,
format,
Expand All @@ -278,7 +290,8 @@ export enum MessageType {
executeCommand,
applyEdit,
appliedEdit,
setWorkspace
setWorkspace,
renameDocument
}

export type AllMessages =
Expand All @@ -304,4 +317,5 @@ export type AllMessages =
| GetCodeActionsMessage
| ExecuteCommandMessage
| AppliedEditMessage
| SetWorkspaceMessage;
| SetWorkspaceMessage
| RenameDocumentMessage;
6 changes: 6 additions & 0 deletions packages/ace-linters/src/services/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ export abstract class BaseService<OptionsType extends ServiceOptions = ServiceOp
}
}

renameDocument(document: lsp.TextDocumentIdentifier, newDocumentUri: string) {
this.documents[newDocumentUri] = this.documents[document.uri];
this.options[newDocumentUri] = this.options[document.uri];
this.removeDocument(document);
}

getDocumentValue(uri: string): string | undefined {
return this.getDocument(uri)?.getText();
}
Expand Down
12 changes: 12 additions & 0 deletions packages/ace-linters/src/services/service-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ export class ServiceManager {
case MessageType.setWorkspace:
this.setWorkspace(message.value);
break;
case MessageType.renameDocument:
this.renameDocument(documentIdentifier, message.value);
break;
}

ctx.postMessage(postMessage);
Expand Down Expand Up @@ -329,6 +332,15 @@ export class ServiceManager {
return services;
}

async renameDocument(documentIdentifier: VersionedTextDocumentIdentifier, newDocumentUri: string) {
let services = this.getServicesInstances(documentIdentifier.uri);
if (services.length > 0) {
services.forEach((el) => el.renameDocument(documentIdentifier, newDocumentUri));
this.$sessionIDToMode[newDocumentUri] = this.$sessionIDToMode[documentIdentifier.uri];
delete this.$sessionIDToMode[documentIdentifier.uri];
}
}

async changeDocumentMode(documentIdentifier: VersionedTextDocumentIdentifier, value: string, mode: string, options: ServiceOptions) {
this.removeDocument(documentIdentifier);
return await this.addDocument(documentIdentifier, value, mode, options);
Expand Down
6 changes: 6 additions & 0 deletions packages/ace-linters/src/types/ace-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ declare module "ace-code/src/tokenizer" {
state: string | string[];
};
}
}

declare module "ace-code/src/document" {
interface Document {
version: number;
}
}
2 changes: 2 additions & 0 deletions packages/ace-linters/src/types/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export interface LanguageService {

removeDocument(document: TextDocumentIdentifier);

renameDocument(document: TextDocumentIdentifier, newDocumentUri: string);

getDocumentValue(uri: string): string | undefined;

provideSignatureHelp(document: lsp.TextDocumentIdentifier, position: lsp.Position): Promise<lsp.SignatureHelp | null>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ export interface IMessageController {
executeCommand(serviceName: string, command: string, args?: any[], callback?: (result: any) => void);

setWorkspace(workspaceUri: string, callback?: () => void): void;

renameDocument(documentIdentifier: ComboDocumentIdentifier, newDocumentUri: string, version: number): void;
}
3 changes: 2 additions & 1 deletion packages/ace-linters/types/language-provider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ declare class SessionLanguageProvider {
private $messageController;
private $deltaQueue;
private $isConnected;
private $modeIsChanged;
private $options?;
private $filePath;
private $isFilePathRequired;
private $servicesCapabilities?;
private $requestsQueue;
state: {
occurrenceMarkers: MarkerGroup | null;
diagnosticMarkers: MarkerGroup | null;
Expand All @@ -129,6 +129,7 @@ declare class SessionLanguageProvider {
* @param messageController - The `IMessageController` instance for handling messages.
*/
constructor(provider: LanguageProvider, session: Ace.EditSession, editor: Ace.Editor, messageController: IMessageController);
enqueueIfNotConnected(callback: () => void): void;
get comboDocumentIdentifier(): ComboDocumentIdentifier;
/**
* @param filePath
Expand Down
1 change: 1 addition & 0 deletions packages/ace-linters/types/message-controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export declare class MessageController implements IMessageController {
getCodeActions(documentIdentifier: ComboDocumentIdentifier, range: lsp.Range, context: lsp.CodeActionContext, callback?: (codeActions: CodeActionsByService[]) => void): void;
executeCommand(serviceName: string, command: string, args?: any[], callback?: (result: any) => void): void;
setWorkspace(workspaceUri: string, callback?: () => void): void;
renameDocument(documentIdentifier: ComboDocumentIdentifier, newDocumentUri: string, version: number): void;
postMessage(message: BaseMessage | CloseConnectionMessage | ExecuteCommandMessage, callback?: (any: any) => void): void;
}
13 changes: 10 additions & 3 deletions packages/ace-linters/types/message-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export declare abstract class BaseMessage {
documentUri: lsp.DocumentUri;
version?: number;
callbackId: number;
protected constructor(documentIdentifier: ComboDocumentIdentifier, callbackId: number);
constructor(documentIdentifier: ComboDocumentIdentifier, callbackId: number);
}
export declare class InitMessage extends BaseMessage {
type: MessageType.init;
Expand Down Expand Up @@ -135,6 +135,12 @@ export declare class AppliedEditMessage {
value: lsp.ApplyWorkspaceEditResult;
constructor(value: lsp.ApplyWorkspaceEditResult, serviceName: string, callbackId: number);
}
export declare class RenameDocumentMessage extends BaseMessage {
type: MessageType.renameDocument;
value: string;
version: number;
constructor(documentIdentifier: ComboDocumentIdentifier, callbackId: number, value: string, version: number);
}
export declare enum MessageType {
init = 0,
format = 1,
Expand All @@ -158,6 +164,7 @@ export declare enum MessageType {
executeCommand = 19,
applyEdit = 20,
appliedEdit = 21,
setWorkspace = 22
setWorkspace = 22,
renameDocument = 23
}
export type AllMessages = InitMessage | FormatMessage | CompleteMessage | ResolveCompletionMessage | ChangeMessage | HoverMessage | ValidateMessage | DeltasMessage | ChangeModeMessage | ChangeOptionsMessage | CloseDocumentMessage | GlobalOptionsMessage | ConfigureFeaturesMessage | SignatureHelpMessage | DocumentHighlightMessage | CloseConnectionMessage | GetSemanticTokensMessage | GetCodeActionsMessage | ExecuteCommandMessage | AppliedEditMessage | SetWorkspaceMessage;
export type AllMessages = InitMessage | FormatMessage | CompleteMessage | ResolveCompletionMessage | ChangeMessage | HoverMessage | ValidateMessage | DeltasMessage | ChangeModeMessage | ChangeOptionsMessage | CloseDocumentMessage | GlobalOptionsMessage | ConfigureFeaturesMessage | SignatureHelpMessage | DocumentHighlightMessage | CloseConnectionMessage | GetSemanticTokensMessage | GetCodeActionsMessage | ExecuteCommandMessage | AppliedEditMessage | SetWorkspaceMessage | RenameDocumentMessage;
1 change: 1 addition & 0 deletions packages/ace-linters/types/services/base-service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare abstract class BaseService<OptionsType extends ServiceOptions = S
addDocument(document: lsp.TextDocumentItem): void;
getDocument(uri: string): TextDocument;
removeDocument(document: lsp.TextDocumentIdentifier): void;
renameDocument(document: lsp.TextDocumentIdentifier, newDocumentUri: string): void;
getDocumentValue(uri: string): string | undefined;
setValue(identifier: lsp.VersionedTextDocumentIdentifier, value: string): void;
setGlobalOptions(options: OptionsType): void;
Expand Down
1 change: 1 addition & 0 deletions packages/ace-linters/types/services/service-manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export declare class ServiceManager {
addDocument(documentIdentifier: VersionedTextDocumentIdentifier, documentValue: string, mode: string, options?: ServiceOptions): Promise<never[] | {
[serviceName: string]: LanguageClientConfig | ServiceConfig;
} | undefined>;
renameDocument(documentIdentifier: VersionedTextDocumentIdentifier, newDocumentUri: string): Promise<void>;
changeDocumentMode(documentIdentifier: VersionedTextDocumentIdentifier, value: string, mode: string, options: ServiceOptions): Promise<never[] | {
[serviceName: string]: LanguageClientConfig | ServiceConfig;
} | undefined>;
Expand Down
5 changes: 5 additions & 0 deletions packages/ace-linters/types/types/ace-extension.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ declare module "ace-code/src/tokenizer" {
};
}
}
declare module "ace-code/src/document" {
interface Document {
version: number;
}
}
1 change: 1 addition & 0 deletions packages/ace-linters/types/types/language-service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface LanguageService {
setGlobalOptions(options: ServiceOptions): any;
getDocument(uri: string): TextDocument;
removeDocument(document: TextDocumentIdentifier): any;
renameDocument(document: TextDocumentIdentifier, newDocumentUri: string): any;
getDocumentValue(uri: string): string | undefined;
provideSignatureHelp(document: lsp.TextDocumentIdentifier, position: lsp.Position): Promise<lsp.SignatureHelp | null>;
findDocumentHighlights(document: lsp.TextDocumentIdentifier, position: lsp.Position): Promise<lsp.DocumentHighlight[]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface IMessageController {
getCodeActions(documentIdentifier: ComboDocumentIdentifier, range: lsp.Range, context: lsp.CodeActionContext, callback?: (codeActions: CodeActionsByService[]) => void): any;
executeCommand(serviceName: string, command: string, args?: any[], callback?: (result: any) => void): any;
setWorkspace(workspaceUri: string, callback?: () => void): void;
renameDocument(documentIdentifier: ComboDocumentIdentifier, newDocumentUri: string, version: number): void;
}