[code] Remove the redundant content in the reference title. (#40481) (#40956)

Related issue: elastic/code#1401 and
elastic/code#1430.

The root cause of this issue is that code server is just designed for
hover results with the `MarkedString[]` format. The other hover results,
like `MarkupContent` hover results are monolithic, we have to extract
pieces of it to construct the reference title.

Although with `MarkupContent` format, there are different patterns,
like go langserver, the pattern is
'comment? + ```go\n + declaration + \n```'
so different languages have different extract patterns.

For the go langserver, the reference title is the first line of the code
block.

For the ctags-langserver, the reference title is the plain text without
markdown part.
This commit is contained in:
Henry Wong 2019-07-12 18:35:32 +08:00 committed by GitHub
parent 0ee210a07a
commit 1b4b8217f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,15 +11,17 @@ import { ResponseError } from 'vscode-jsonrpc';
import { ResponseMessage } from 'vscode-jsonrpc/lib/messages';
import { Location } from 'vscode-languageserver-types';
import {
LanguageServerStartFailed,
ServerNotInitialized,
UnknownFileLanguage,
LanguageServerStartFailed,
} from '../../common/lsp_error_codes';
import { parseLspUrl } from '../../common/uri_util';
import { GitOperations } from '../git_operations';
import { Logger } from '../log';
import { CTAGS, GO } from '../lsp/language_servers';
import { LspService } from '../lsp/lsp_service';
import { SymbolSearchClient } from '../search';
import { CodeServerRouter } from '../security';
import { ServerOptions } from '../server_options';
import {
expandRanges,
@ -30,7 +32,6 @@ import {
import { detectLanguage } from '../utils/detect_language';
import { EsClientWithRequest } from '../utils/esclient_with_request';
import { promiseTimeout } from '../utils/timeout';
import { CodeServerRouter } from '../security';
const LANG_SERVER_ERROR = 'language server error';
@ -109,9 +110,29 @@ export function lspRoute(
});
let title: string;
if (hover.result && hover.result.contents) {
title = Array.isArray(hover.result.contents)
? hover.result.contents[0].value
: (hover.result.contents as 'string');
if (Array.isArray(hover.result.contents)) {
const content = hover.result.contents[0];
title = hover.result.contents[0].value;
const lang = await detectLanguage(uri.replace('file://', ''));
// TODO(henrywong) Find a gernal approach to construct the reference title.
if (content.kind) {
// The format of the hover result is 'MarkupContent', extract appropriate pieces as the references title.
if (GO.languages.includes(lang)) {
title = title.substring(title.indexOf('```go\n') + 5, title.lastIndexOf('\n```'));
if (title.includes('{\n')) {
title = title.substring(0, title.indexOf('{\n'));
}
}
} else if (CTAGS.languages.includes(lang)) {
// There are language servers may provide hover results with markdown syntax, like ctags-langserver,
// extract the plain text.
if (title.substring(0, 2) === '**' && title.includes('**\n')) {
title = title.substring(title.indexOf('**\n') + 3);
}
}
} else {
title = hover.result.contents as 'string';
}
} else {
title = last(uri.toString().split('/')) + `(${position.line}, ${position.character})`;
}