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: add reference and definition provider #145

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Cythia828
Copy link
Collaborator

@Cythia828 Cythia828 commented Sep 25, 2024

feat

支持 #162 跳转到定义、跳转到引用位置功能

  • 支持跳转到引用、跳转到定义
  • 支持外部控制跳转到引用、跳转到定义配置的开关,如下图所示:
image

需要注意

  • 跳转到引用的默认快捷键是Shift+F12,但由于大家的使用习惯是Command+click,所以需要关注下

当你在 Monaco Editor 中配置 references 为 true 而 definitions 为 false 时,Command + Click (或 Ctrl + Click) 无法跳转到引用,这是因为 Monaco Editor 的默认行为是:Command + Click 主要用于跳转到 定义 (definitions),而不是引用 (references)。 即使你启用了 references 功能,Command + Click 仍然优先处理 DefinitionProvider,因为它被设计为跳转到定义的快捷方式。

ReferenceProvider 提供的是“查找所有引用”的功能,这通常通过右键菜单或自定义命令来触发,而不是 Command + Click。 Command + Click 的行为是由 Monaco Editor 的核心功能决定的,它与 ReferenceProvider 和 DefinitionProvider 的启用状态之间没有直接的逻辑关联。 Command + Click 始终优先尝试跳转到定义,只有当 DefinitionProvider 无法提供定义时,它才可能尝试其他行为。

常规使用跳转到定义、引用功能时,我们都是配套打开的,此时,我们通过Command + Click 可以进行跳转到定义和引用。通常不会只打开跳转到引用,而不打开跳转到定义。如果只打开跳转到引用,而不打开跳转到定义,那么我们通过 右键唤起菜单跳到引用,通过选择某一个具体引用进行跳转,这个操作也是流畅且合理的。

预览

iShot_2024-12-02_18 01 39

预览地址

https://cythia828.github.io/monaco-sql-languages/

@liuxy0551
Copy link
Collaborator

How can I use them?

@Cythia828
Copy link
Collaborator Author

How can I use them?

这两个provider作为基础功能已经为你注册好了,在你编辑sql的时候,在光标实体处按住command加点击,如果当前实体是引用实体,就会跳转到其定义的地方(即CREATE的地方);如果当前实体是定义实体,则会显示出使用了当前实体的地方,如下图

image

): languages.ProviderResult<languages.Definition | languages.LocationLink[]> {
const resource = model.uri;
const lineContent = model.getLineContent(position.lineNumber);
if (lineContent.startsWith('--')) return null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是否还要考虑块级注释

/* DROP TABLE IF EXISTS aabb; */
-- DROP TABLE IF EXISTS aabb;
DROP TABLE IF EXISTS aabb;

@@ -197,3 +201,85 @@ export class CompletionAdapter<T extends BaseSQLWorker>
});
}
}

export class DefinitionAdapter<T extends BaseSQLWorker> implements languages.DefinitionProvider {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看下两个 Provider 要不要加下注释

if (!lineContent.startsWith('CREATE')) return null;
return this._worker(resource)
.then((worker) => {
return worker.getAllEntities(model.getValue());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code 需要用 preprocessCode 处理,原因参考 #60

另一个 provider 同

@@ -34,6 +34,18 @@ export function setupLanguageMode<T extends BaseSQLWorker>(
)
);
}
providers.push(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑是否支持配置为关闭此功能,参考已有的自动补全功能和代码飘红功能,都是可以关闭的

.then((entities) => {
const word = model.getWordAtPosition(position);
const arr: languages.Location[] = [];
entities?.forEach((entity) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑是否支持外部自定义跳转位置,另一个provider同

@HaydenOrz
Copy link
Collaborator

HaydenOrz commented Oct 21, 2024

deploy website到自己的仓库上,让review的人可以预览

@liuxy0551
Copy link
Collaborator

liuxy0551 commented Oct 22, 2024

deploy website到自己的仓库上,让review的人可以预览

@Cythia828package.json "deploy" 命令中的 "DTStack" 改为自己的名称,再执行 npm run deploy

@Cythia828
Copy link
Collaborator Author

deploy website到自己的仓库上,让review的人可以预览

@Cythia828package.json "deploy" 命令中的 "DTStack" 改为自己的名称,再执行 npm run deploy

@Cythia828 Cythia828 closed this Oct 22, 2024
@Cythia828 Cythia828 reopened this Oct 22, 2024
@Cythia828 Cythia828 force-pushed the feat_reference_definition branch 3 times, most recently from 540687d to 59a541e Compare November 14, 2024 02:52
@Cythia828 Cythia828 force-pushed the feat_reference_definition branch from 59a541e to ee61c06 Compare November 14, 2024 02:54
word?.word &&
entity.text === word?.word
) {
pos = entity.position;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

找到后可以终端循环

if (
entity.entityContextType.includes('Create') &&
word?.word &&
entity.text === word?.word
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果字段实体和表实体重名了,那也会被跳转到定义

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

Successfully merging this pull request may close these issues.

4 participants