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

feat: support progress window for indexing #2

Merged
merged 1 commit into from
Mar 9, 2021
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- `intelephense.enable`: Enable coc-intelephense extension, default `true`
- `intelephense.path`: Absolute path to intelephense module. If there is no setting, the built-in module will be used. e.g. `/path/to/node_modules/intelephense`. default: ""
- `intelephense.disableCompletion`: Disable completion only, default: `false`
- `intelephense.progress.enable`: Enable progress window for indexing, If false, display with echo messages, default: `true`

Other settings can be changed in the same way as "configuration" of [vscode-intelephense](https://github.com/bmewburn/vscode-intelephense).

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
"default": false,
"description": "Disable completion only."
},
"intelephense.progress.enable": {
"type": "boolean",
"default": true,
"description": "Enable progress window for indexing, If false, display with echo messages."
},
"intelephense.compatibility.correctForBaseClassStaticUnionTypes": {
"type": "boolean",
"default": true,
Expand Down
61 changes: 50 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,7 @@ function createClient(context: ExtensionContext, clearCache: boolean) {
const languageClient = new LanguageClient('intelephense', 'intelephense', serverOptions, clientOptions);

languageClient.onReady().then(() => {
let startedTime: Date;

languageClient.onNotification(INDEXING_STARTED_NOTIFICATION.method, () => {
startedTime = new Date();
window.showMessage('intelephense indexing ...');
});

languageClient.onNotification(INDEXING_ENDED_NOTIFICATION.method, () => {
const usedTime: number = Math.abs(new Date().getTime() - startedTime.getTime());
window.showMessage('Indexed php files, times: ' + usedTime + 'ms');
});
registerNotificationListeners();
});

return languageClient;
Expand All @@ -125,3 +115,52 @@ function indexWorkspace() {
function cancelIndexing() {
languageClient.sendRequest(CANCEL_INDEXING_REQUEST.method);
}

// MEMO: support progress window for indexing
function registerNotificationListeners() {
const intelephenseConfig = workspace.getConfiguration('intelephense');
const progressEnable = intelephenseConfig.get<boolean>('progress.enable');

let resolveIndexingPromise: () => void;

if (progressEnable) {
languageClient.onNotification(INDEXING_STARTED_NOTIFICATION.method, () => {
displayInitIndexProgress(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
new Promise<void>((resolve, reject) => {
resolveIndexingPromise = () => {
resolve();
};
})
);
});

languageClient.onNotification(INDEXING_ENDED_NOTIFICATION.method, () => {
if (resolveIndexingPromise) {
resolveIndexingPromise();
}
});
} else {
languageClient.onNotification(INDEXING_STARTED_NOTIFICATION.method, () => {
window.showMessage('intelephense indexing ...');
});

languageClient.onNotification(INDEXING_ENDED_NOTIFICATION.method, () => {
if (resolveIndexingPromise) {
resolveIndexingPromise();
}
window.showMessage('intelephense running!');
});
}
}

// MEMO: support progress window for indexing
async function displayInitIndexProgress<T = void>(promise: Promise<T>) {
return window.withProgress(
{
title: 'intelephense indexing ...',
cancellable: true,
},
() => promise
);
}