-
Notifications
You must be signed in to change notification settings - Fork 49
feat: support hover provider feat #194
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
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import type { ParseError } from 'dt-sql-parser'; | ||
| import { ColumnEntityContext, CommonEntityContext, EntityContextType } from 'dt-sql-parser'; | ||
| import { EntityContext } from 'dt-sql-parser/dist/parser/common/entityCollector'; | ||
| import { WordPosition } from 'dt-sql-parser/dist/parser/common/textAndWord'; | ||
| import * as monaco from 'monaco-editor'; | ||
|
|
@@ -17,6 +18,16 @@ import { | |
| } from './fillers/monaco-editor-core'; | ||
| import type { CompletionSnippet, LanguageServiceDefaults } from './monaco.contribution'; | ||
|
|
||
| export interface ColumnInfo { | ||
| /** 字段名 */ | ||
| column: string; | ||
| /** 字段类型 */ | ||
| type: string | undefined; | ||
| /** 注释 */ | ||
| comment?: string; | ||
| /** 别名 */ | ||
| alias?: string; | ||
| } | ||
| export interface WorkerAccessor<T extends BaseSQLWorker> { | ||
| (...uris: Uri[]): Promise<T>; | ||
| } | ||
|
|
@@ -352,3 +363,131 @@ export class ReferenceAdapter<T extends BaseSQLWorker> implements languages.Refe | |
| }); | ||
| } | ||
| } | ||
| /** | ||
| * The adapter is for the hover provider interface defines the contract between extensions and | ||
| * the [hover](https://code.visualstudio.com/docs/editor/intellisense)-feature. | ||
| **/ | ||
| export class HoverAdapter<T extends BaseSQLWorker> implements languages.HoverProvider { | ||
| constructor( | ||
| private readonly _worker: WorkerAccessor<T>, | ||
| private readonly _defaults: LanguageServiceDefaults | ||
| ) {} | ||
| provideHover( | ||
| model: editor.IReadOnlyModel, | ||
| position: Position, | ||
| _token: CancellationToken | ||
| ): languages.ProviderResult<languages.Hover> { | ||
| const resource = model.uri; | ||
| const lineContent = model.getLineContent(position.lineNumber); | ||
| if (lineContent.trim().startsWith('--')) return null; | ||
| return this._worker(resource) | ||
| .then((worker) => { | ||
| let code = model?.getValue() || ''; | ||
| if (typeof this._defaults.preprocessCode === 'function') { | ||
| code = this._defaults.preprocessCode(code); | ||
| } | ||
| return worker.getAllEntities(code, position); | ||
| }) | ||
| .then((entities) => { | ||
| if (!entities || !entities.length) return null; | ||
| let isAlias = false; | ||
| const curEntity = entities.find((entity: EntityContext) => { | ||
| const p = entity.position; | ||
| const alias = entity._alias; | ||
| isAlias = !!( | ||
| alias && | ||
| alias.startColumn <= position.column && | ||
| alias.endColumn >= position.column && | ||
| alias.line === position.lineNumber | ||
| ); | ||
| return ( | ||
| (p.startColumn <= position.column && | ||
| p.endColumn >= position.column && | ||
| p.line === position.lineNumber) || | ||
| isAlias | ||
| ); | ||
| }); | ||
| if (!curEntity) return null; | ||
| const tableCreate = findTableCreateEntity(curEntity, entities); | ||
| const columns = tableCreate ? toColumnsInfo(tableCreate) || [] : []; | ||
| const columnsDesc = columns.reduce((res, cur) => { | ||
| const { column, type, comment, alias } = cur; | ||
| return ( | ||
| res + | ||
| `\`${column}\` ${type ? ` **${type}**` : ''} ${ | ||
| comment ? ` *${comment}*` : '' | ||
| } ${alias ? ` *${alias}*` : ''} \n` | ||
| ); | ||
| }, ''); | ||
| const tableText = isAlias | ||
| ? (curEntity._alias?.text ?? curEntity.text) | ||
| : curEntity.text; | ||
| let range: monaco.Range; | ||
| if (isAlias && curEntity._alias) { | ||
| range = new monaco.Range( | ||
| curEntity._alias.line, | ||
| curEntity._alias.startColumn, | ||
| curEntity._alias.line, | ||
| curEntity._alias.endColumn | ||
| ); | ||
| } else { | ||
| const p = curEntity.position; | ||
| range = new monaco.Range(p.line, p.startColumn, p.line, p.endColumn); | ||
| } | ||
| const contents: monaco.IMarkdownString[] = [ | ||
| { value: `**${tableText}**` }, | ||
| { value: columnsDesc } | ||
| ]; | ||
| return { contents, range }; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * According to the table name or table entity field, get the corresponding create table information | ||
| */ | ||
| export function findTableCreateEntity( | ||
| tableEntity: EntityContext | string, | ||
| allEntities: EntityContext[] | ||
| ): CommonEntityContext | null { | ||
| if ( | ||
| typeof tableEntity !== 'string' && | ||
| tableEntity.entityContextType !== EntityContextType.TABLE | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| const tableName: string = typeof tableEntity === 'string' ? tableEntity : tableEntity.text; | ||
| function removeQuotes(str: string): string { | ||
| return str.replace(/^['"`“”‘’«»]|['"`“”‘’«»]$/g, ''); | ||
| } | ||
| return ( | ||
| allEntities.find( | ||
| (en): en is CommonEntityContext => | ||
| en.entityContextType === EntityContextType.TABLE_CREATE && | ||
| removeQuotes(en.text) === removeQuotes(tableName) | ||
| ) ?? null | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 考虑实体一边加反引号,一边没加的情况应该也匹配上 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这种情况我调试一下看看 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 加了加了 |
||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Transform table create entity to columns info | ||
| */ | ||
| export function toColumnsInfo(tableEntity: CommonEntityContext): ColumnInfo[] | null { | ||
| if ( | ||
| !tableEntity || | ||
| tableEntity.entityContextType !== EntityContextType.TABLE_CREATE || | ||
| !tableEntity.columns?.length | ||
| ) | ||
| return null; | ||
| const columnsInfo: ColumnInfo[] = []; | ||
| tableEntity.columns.forEach((col: ColumnEntityContext) => { | ||
| columnsInfo.push({ | ||
| column: col.text, | ||
| type: col._colType?.text, | ||
| comment: col._comment?.text, | ||
| alias: col._alias?.text | ||
| }); | ||
| }); | ||
| return columnsInfo; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当hover在表别名上时,也可以查找到的,展示的时候可以把表和别名一起展示
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实,我加一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加了