Skip to content

Commit 3ded0d3

Browse files
authored
feat: Enhancement of code document. (#57)
1 parent d2f816b commit 3ded0d3

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release Notes
22

3+
## 0.0.10(2024-05-16)
4+
5+
- SDK code documentation has been enhanced with the Code Sample link.
6+
- Fixed some UI issues.
7+
38
## 0.0.9(2024-04-22)
49

510
- The UI was optimized and a deprecated tag was added to indicate that the API is not recommended.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ Or use the keyboard shortcuts `ctrl+cmd+l` to search the API and select Insert s
7878

7979
![code snippets](https://img.alicdn.com/imgextra/i3/O1CN01dmGwmX1ZyVHozyKx4_!!6000000003263-1-tps-842-468.gif)
8080

81+
### Document enhancement
82+
83+
When writing SDK code, you can get more code sample references by viewing the OpenAPI description information and additional related sample links through the code documentation.
84+
85+
![Document enhancement](https://img.alicdn.com/imgextra/i3/O1CN01Yj1PrE1qwzTj3cFn8_!!6000000005561-0-tps-1562-518.jpg)
86+
8187
## Feedback
8288

8389
- Submit bug reports and feature requests on [our Github repository](https://github.com/aliyun/alibabacloud-api-vscode-toolkit/issues).

README.zh_CN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ Default Language [zh]: zh
7979

8080
![code snippets](https://img.alicdn.com/imgextra/i3/O1CN01dmGwmX1ZyVHozyKx4_!!6000000003263-1-tps-842-468.gif)
8181

82+
### 文档增强
83+
84+
你能够在编写 SDK 代码时,通过代码文档看到 OpenAPI 的描述信息以及更多相关示例链接,来获得更多代码示例参考。
85+
86+
![Document enhancement](https://img.alicdn.com/imgextra/i3/O1CN01Yj1PrE1qwzTj3cFn8_!!6000000005561-0-tps-1562-518.jpg)
87+
8288
## 反馈
8389

8490
- 欢迎在我们的 [Github repository](https://github.com/aliyun/alibabacloud-api-vscode-toolkit/issues) 上提交你的问题和建议。

src/Service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ export class AlicloudAPIService {
141141
return {};
142142
}
143143

144+
/**
145+
* @description 根据 API 查询是否有 CodeSample
146+
*/
147+
async requestSamplesByAPI(product: string, version: string, api: string) {
148+
const resStr = await fetch(
149+
`https://api.aliyun.com/api/samples/product/${product}/version/${version}/api/${api}`,
150+
{},
151+
).then((res) => res.text());
152+
const res = JSON.parse(resStr);
153+
return res?.data || [];
154+
}
155+
144156
async requestEndpoints(product: string) {
145157
const resStr = await fetch(
146158
`https://api.aliyun.com/meta/v1/products/${product}/endpoints.json?language=zh-CN`,

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { AlicloudApiMetaGeneratePlugin } from "./plugins/generate";
1414
import { getProductRequestInstance } from "./productExplorer";
1515
import autoCompletion from "./provider/autoCompletion";
1616
import autofix from "./provider/autofix";
17+
import hoverInfo from "./provider/hoverProvider";
1718

1819
export async function activate(context: vscode.ExtensionContext) {
1920
// if (!vscode.workspace.rootPath) {
@@ -81,6 +82,8 @@ export async function activate(context: vscode.ExtensionContext) {
8182
autoCompletion(context);
8283
// 自动修复
8384
autofix(context);
85+
// hover提示
86+
hoverInfo(context);
8487
}
8588
} catch (e) {
8689
vscode.window.showErrorMessage(e.message);

src/provider/hoverProvider.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @author: yini-chen
3+
* @description: hover provider
4+
*/
5+
6+
import * as vscode from "vscode";
7+
import { fileSel, getSpecInfoFromName } from "../utils";
8+
import { AlicloudAPIService, alicloudAPIMessageService } from "../Service";
9+
import { AlicloudApiCommands } from "../commands";
10+
import { getProductRequestInstance } from "../productExplorer";
11+
12+
const getKeyWord = (word: string) => {
13+
if (/^[A-Za-z].*Request$/.test(word)) {
14+
return word.replace("Request", "");
15+
}
16+
return word;
17+
};
18+
19+
const getProductsKeywords = (productCode: string, versions: Array<string>): Array<string> => {
20+
const keywords = versions?.map((version) => {
21+
const newVersion = version?.split("-")?.join("");
22+
return `${productCode}${newVersion}`.toLocaleLowerCase();
23+
});
24+
const pythonKeywords = keywords?.map((key) => {
25+
return `${key}client`;
26+
});
27+
keywords.push(productCode.toLocaleLowerCase());
28+
return [...keywords, ...pythonKeywords];
29+
};
30+
31+
class HoverProvider {
32+
async provideHover(document: vscode.TextDocument, position: vscode.Position) {
33+
const service = alicloudAPIMessageService;
34+
const apis = service.pontManager.localPontSpecs
35+
.map((pontSpec) => {
36+
return AlicloudApiCommands.getPickItems(pontSpec);
37+
})
38+
.reduce((pre, next) => pre.concat(next), []);
39+
40+
const wordRange = document.getWordRangeAtPosition(position);
41+
const word = document.getText(wordRange);
42+
const keyWord = getKeyWord(word);
43+
44+
const hoverdAPI = apis?.find((item) => item?.label?.toLocaleLowerCase() === keyWord?.toLocaleLowerCase());
45+
const productInstance = await getProductRequestInstance();
46+
const hoverdProduct = productInstance?.products?.find((item) =>
47+
getProductsKeywords(item.code, item.versions)?.includes(keyWord?.toLocaleLowerCase()),
48+
);
49+
// 匹配关键字为 API 名称
50+
if (hoverdAPI) {
51+
const apiName = hoverdAPI?.label;
52+
const { product, version } = getSpecInfoFromName(hoverdAPI?.info?.split("/")[0]);
53+
const service = new AlicloudAPIService();
54+
const samples = await service.requestSamplesByAPI(product, version, keyWord);
55+
return new vscode.Hover([
56+
samples?.length
57+
? `💡 [查看更多「${apiName}」相关代码示例](https://api.aliyun.com/api/${product}/${version}/${apiName}?tab=CodeSample)`
58+
: `💡 [查看更多「${product}」的相关代码示例](https://api.aliyun.com/api-tools/demo/${product})`,
59+
hoverdAPI?.summary || "",
60+
]);
61+
}
62+
// 匹配关键字为产品名称
63+
if (hoverdProduct) {
64+
return new vscode.Hover([
65+
`💡 [查看更多「${hoverdProduct?.name || hoverdProduct?.code}」的相关代码示例](https://api.aliyun.com/api-tools/demo/${hoverdProduct?.code})`,
66+
hoverdProduct?.description || hoverdProduct?.name,
67+
]);
68+
}
69+
}
70+
}
71+
72+
export default function (context: vscode.ExtensionContext) {
73+
context.subscriptions.push(vscode.languages.registerHoverProvider(fileSel, new HoverProvider()));
74+
}

0 commit comments

Comments
 (0)