Skip to content

Commit bfeb34e

Browse files
Merge pull request #5 from shezhangzhang/fix/wrong-decorations
release v0.3.0
2 parents 5dc8c75 + 932832a commit bfeb34e

File tree

5 files changed

+105
-24
lines changed

5 files changed

+105
-24
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "antd-design-token",
33
"displayName": "antd Design Token",
44
"description": "VSCode extension for antd v5 design token.",
5-
"version": "0.2.3",
5+
"version": "0.3.0",
66
"publisher": "shezhangzhang",
77
"engines": {
88
"vscode": "^1.68.0"
@@ -59,7 +59,7 @@
5959
"webpack-cli": "^4.9.2"
6060
},
6161
"dependencies": {
62-
"antd-token-previewer": "^1.0.0-alpha.20",
62+
"antd-token-previewer": "^1.0.0-alpha.22",
6363
"rgb-hex": "^4.0.0"
6464
},
6565
"repository": {

src/decoration-manager.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export default class DecorationManager {
122122
}
123123

124124
fullTokenKeys.forEach((key: string) => {
125-
const regEx = new RegExp(`\\b(${key})\\b(?!-)`, "g");
125+
const regEx = new RegExp(`\\b(${key})\\b(?!-|:)`, "g");
126126
let match;
127127

128128
while ((match = regEx.exec(text))) {
@@ -245,12 +245,8 @@ export default class DecorationManager {
245245
}
246246

247247
clearAllFileDecorations() {
248-
console.log(11111);
249-
console.log(this);
250-
console.log(this.fileDecorationMap);
251248
if (this.fileDecorationMap?.size) {
252249
this.fileDecorationMap.forEach((value, key) => {
253-
console.log("key", key);
254250
this.clearFileDecoration(key);
255251
});
256252
}

src/extension.ts

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
import getDesignToken from "antd-token-previewer/es/utils/getDesignToken";
22
import * as vscode from "vscode";
3-
import setupEventListener, { DisposableAndClear } from "./listener";
3+
import setupEventListenerAndDecorations, {
4+
DisposableAndClear,
5+
} from "./listener";
46
import setupAntdTokenCompletion from "./typing";
7+
import { checkAntdProject } from "./utils";
58

69
export function activate(context: vscode.ExtensionContext) {
7-
let isActive = true;
8-
let disposeTyping: vscode.Disposable;
9-
let disposableAndClear: DisposableAndClear;
10+
const ACTIVE_KEY = "antd-design-token-active-key";
11+
const isActive = context.globalState.get(ACTIVE_KEY);
12+
let disposeTyping: vscode.Disposable | undefined;
13+
let disposableAndClear: DisposableAndClear | undefined;
1014

11-
setup();
15+
if (isActive || isActive === undefined) {
16+
setup();
17+
}
1218

1319
const disposable = vscode.commands.registerCommand(
1420
"antd-design-token.toggle",
1521
() => {
16-
isActive = !isActive;
22+
const isActive = context.globalState.get(ACTIVE_KEY);
23+
context.globalState.update(ACTIVE_KEY, !isActive);
1724

1825
if (isActive) {
19-
setup();
26+
disposeAll();
2027
vscode.window.showInformationMessage(
21-
"antd design token is active now."
28+
"antd design token is inactive now."
2229
);
2330
} else {
24-
disposeTyping.dispose();
25-
disposableAndClear.disposable.forEach((disposable) =>
26-
disposable.dispose()
27-
);
28-
disposableAndClear.clear();
31+
setup();
2932
vscode.window.showInformationMessage(
30-
"antd design token is inactive now."
33+
"antd design token is active now."
3134
);
3235
}
3336
}
@@ -43,8 +46,55 @@ export function activate(context: vscode.ExtensionContext) {
4346
return;
4447
}
4548

49+
activeEditorListener(fullToken);
50+
51+
const isAntdProject = checkAntdProject();
52+
if (isAntdProject) {
53+
setupDecorationsAndCompletion(context, fullToken);
54+
}
55+
}
56+
57+
function setupDecorationsAndCompletion(
58+
context: vscode.ExtensionContext,
59+
fullToken: any
60+
) {
4661
disposeTyping = setupAntdTokenCompletion(fullToken);
47-
disposableAndClear = setupEventListener(context, fullToken);
62+
disposableAndClear = setupEventListenerAndDecorations(context, fullToken);
63+
}
64+
65+
function disposeAll() {
66+
if (disposeTyping) {
67+
disposeTyping?.dispose();
68+
disposeTyping = undefined;
69+
}
70+
71+
if (disposableAndClear) {
72+
disposableAndClear?.disposable?.forEach((disposable) =>
73+
disposable?.dispose()
74+
);
75+
disposableAndClear?.clear();
76+
disposableAndClear = undefined;
77+
}
78+
}
79+
80+
function activeEditorListener(fullToken: any) {
81+
vscode.window.onDidChangeActiveTextEditor(
82+
(editor) => {
83+
if (editor) {
84+
const isAntdProject = checkAntdProject();
85+
86+
if (isAntdProject) {
87+
if (!disposeTyping && !disposableAndClear) {
88+
setupDecorationsAndCompletion(context, fullToken);
89+
}
90+
} else {
91+
disposeAll();
92+
}
93+
}
94+
},
95+
null,
96+
context.subscriptions
97+
);
4898
}
4999
}
50100

src/listener.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as vscode from "vscode";
22
import DecorationManager from "./decoration-manager";
3+
import { checkAntdProject } from "./utils";
34

45
export interface DisposableAndClear {
56
disposable: vscode.Disposable[];
67
clear: () => void;
78
}
89

9-
export default function setupEventListener(
10+
export default function setupEventListenerAndDecorations(
1011
context: vscode.ExtensionContext,
1112
fullToken: any
1213
): DisposableAndClear {
@@ -18,6 +19,7 @@ export default function setupEventListener(
1819
if (activeEditor) {
1920
fileLineCount = activeEditor.document.lineCount;
2021
decorationManager.setActiveEditor(activeEditor);
22+
console.log(44444);
2123
decorationManager.triggerUpdateDecorations();
2224
}
2325

@@ -60,6 +62,7 @@ export default function setupEventListener(
6062

6163
const disposableActiveChange = vscode.window.onDidChangeActiveTextEditor(
6264
(editor) => {
65+
console.log("change!!!!!");
6366
activeEditor = editor;
6467
if (editor) {
6568
fileLineCount = editor.document.lineCount || 0;

src/utils/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import rgbHex from "rgb-hex";
2-
2+
import * as vscode from "vscode";
3+
import * as fs from "fs";
4+
import * as path from "path";
35
/**
46
* Generate the MarkdownString for vscode.MarkdownString function
57
* Note: should convert rgb color to hex
@@ -29,3 +31,33 @@ export function getColorTokenValue(value: string): string {
2931

3032
return result;
3133
}
34+
35+
export function checkAntdProject(): boolean {
36+
const projectPath = getProjectPath();
37+
console.log("projectPath", projectPath);
38+
39+
if (projectPath) {
40+
const pkgFilePath = path.join(projectPath, "/package.json");
41+
42+
if (fs.existsSync(pkgFilePath)) {
43+
const packegeJson = fs.readFileSync(pkgFilePath);
44+
if (
45+
packegeJson.toString().includes("antd") ||
46+
packegeJson.toString().includes("rc-")
47+
) {
48+
return true;
49+
}
50+
}
51+
}
52+
53+
return false;
54+
}
55+
56+
export function getProjectPath(): string | undefined {
57+
const fileName = vscode.window.activeTextEditor?.document?.fileName;
58+
console.log("filename", fileName);
59+
60+
return vscode.workspace.workspaceFolders
61+
?.map((folder) => folder.uri.fsPath)
62+
.filter((fsPath) => fileName?.startsWith(fsPath))[0];
63+
}

0 commit comments

Comments
 (0)