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

How to get the lsp response in vscode-language-client? #628

Closed
Abbyyan opened this issue Jun 4, 2020 · 11 comments
Closed

How to get the lsp response in vscode-language-client? #628

Abbyyan opened this issue Jun 4, 2020 · 11 comments

Comments

@Abbyyan
Copy link

Abbyyan commented Jun 4, 2020

I'm working on a vscode lsp extension development and want to ask if there are some methods that i can get the lsp response in the vscode-language-client ? Hope for your help. Thanks a lot.

@dbaeumer
Copy link
Member

dbaeumer commented Jun 4, 2020

@Abbyyan the LSP client has a middleware which all requests and response go through.

@dbaeumer dbaeumer closed this as completed Jun 4, 2020
@Abbyyan
Copy link
Author

Abbyyan commented Jun 4, 2020

How can i get the full completion items in the server response? Should i use provideCompletionItem or resolveCompletionItem ? Thanks for your help.

@Abbyyan
Copy link
Author

Abbyyan commented Jun 4, 2020

Is there some documents shows how to use them please?

@rcjsuen
Copy link
Contributor

rcjsuen commented Jun 4, 2020

How can i get the full completion items in the server response? Should i use provideCompletionItem or resolveCompletionItem ? Thanks for your help.

You probably want the former but you may want both depending on your exact use case.

Is there some documents shows how to use them please?

Maybe the code from #322 may serve as a bit of a reference for you.

@Abbyyan
Copy link
Author

Abbyyan commented Jun 4, 2020

Learned from lsp-example that i can change the input send to server (just as the uri in the example). But i still confused about get the response (or other info ) from server. For example , how can i get the completion list in provideCompletionItem? Hope for your answer please. Thanks for your help!

let clientOptions: LanguageClientOptions = {
		documentSelector: [{ scheme: 'file', language: 'html1' }],
		middleware: {
			provideCompletionItem: async (document, position, context, token, next) => {
				// If not in `<style>`, do not perform request forwarding
				if (!isInsideStyleRegion(htmlLanguageService, document.getText(), document.offsetAt(position))) {
					return await next(document, position, context, token);
				}

				const originalUri = document.uri.toString();
				virtualDocumentContents.set(originalUri, getCSSVirtualContent(htmlLanguageService, document.getText()));

				const vdocUriString = `embedded-content://css/${encodeURIComponent(
					originalUri
				)}.css`;
				const vdocUri = Uri.parse(vdocUriString);
				return await commands.executeCommand<CompletionList>(
					'vscode.executeCompletionItemProvider',
					vdocUri,
					position,
					context.triggerCharacter
				);
			}
		}
	};

@dbaeumer
Copy link
Member

dbaeumer commented Jun 4, 2020

The result of next(document, position, context, token) is the result from the LSP server. It is like next handlers in web servers.

@Abbyyan
Copy link
Author

Abbyyan commented Jun 4, 2020

The result of next(document, position, context, token) is the result from the LSP server. It is like next handlers in web servers.

Thanks for your help. It really works! Thanks a lot. By using next , I can get the response now . Thanks a lot.

provideCompletionItem: async (document: TextDocument,
                                      position: Position, context: CompletionContext,
                                      token: CancellationToken, next: ProvideCompletionItemsSignature) => {
          if (true) {
            const temp_result: any = await next(document, position, context, token);
            if (temp_result) {
              const temp_result_info: CompletionList = temp_result;
              logChan("temp_result is not null " + temp_result_info.toLocaleString);
              const items: any = temp_result_info.items;
              items.forEach((element: CompletionItem) => {
                logChan("completion element label = " + element.label + "  detail = " + element.detail);
              });
            }
            return temp_result;
          }
          return [];
        }
      },

@Abbyyan
Copy link
Author

Abbyyan commented Jun 4, 2020

The result of next(document, position, context, token) is the result from the LSP server. It is like next handlers in web servers.
How can i send the Intercepted and processed response to the vscode client?
Should i get the next function result by await next, then process the response and send the processed response to vscode client by return processed_response in the provideCompletionItem function just like follows?

provideCompletionItem: async (document: TextDocument,
                                      position: Position, context: CompletionContext,
                                      token: CancellationToken, next: ProvideCompletionItemsSignature) => {
            const temp_result: any = await next(document, position, context, token);
            const processed_result = some_process_func(temp_result)
            return processed_result;
        }

I've tried it but it seems something doesn't work like this. Hope for your advice . Thanks a lot.

@rcjsuen
Copy link
Contributor

rcjsuen commented Jun 4, 2020

I've tried it but it seems something doesn't work like this. Hope for your advice . Thanks a lot.

@Abbyyan I don't understand. You just said it worked an hour ago (#628 (comment))?

@Abbyyan
Copy link
Author

Abbyyan commented Jun 4, 2020

I've tried it but it seems something doesn't work like this. Hope for your advice . Thanks a lot.

@Abbyyan I don't understand. You just said it worked an hour ago (#628 (comment))?

Yes, I can get the completion list can print it out. I intercept the response and process it . It confused me now that how can i send my processed response to vscode client? Thanks a lot.

@rcjsuen
Copy link
Contributor

rcjsuen commented Jun 4, 2020

I intercept the response and process it . It confused me now that how can i send my processed response to vscode client?

Aren't you doing that already with your return processed_result; line?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants