Skip to content

Commit

Permalink
refactor: more
Browse files Browse the repository at this point in the history
refactor: more

refactor: more

refactor: more
  • Loading branch information
yuqizhou77 committed Jul 3, 2024
1 parent 5d5b9ac commit 3c6e095
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 75 deletions.
1 change: 1 addition & 0 deletions packages/fx-core/src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class FeatureFlagName {
static readonly NewGenerator = "TEAMSFX_NEW_GENERATOR";
static readonly SMEOAuth = "SME_OAUTH";
static readonly CustomizeGpt = "TEAMSFX_DECLARATIVE_COPILOT";
static readonly ShowDiagnostics = "TEAMSFX_SHOW_DIAGNOSTICS";
}

export function getAllowedAppMaps(): Record<string, string> {
Expand Down
4 changes: 4 additions & 0 deletions packages/fx-core/src/common/featureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export class FeatureFlags {
};
static readonly SMEOAuth = { name: FeatureFlagName.SMEOAuth, defaultValue: "false" };
static readonly CustomizeGpt = { name: FeatureFlagName.CustomizeGpt, defaultValue: "false" };
static readonly ShowDiagnostics = {
name: FeatureFlagName.ShowDiagnostics,
defaultValue: "false",
};
}

export class FeatureFlagManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
Platform,
Result,
TeamsAppManifest,
UserInteraction,
err,
ok,
} from "@microsoft/teamsfx-api";
Expand Down Expand Up @@ -303,9 +302,6 @@ export class ValidateAppPackageDriver implements StepDriver {
"driver.teamsApp.validate.result.display",
summaryStr.join(", ")
);
if (context.platform === Platform.VSCode && errors.length > 0) {
context.ui?.showDiagnosticInfo!([]);
}
if (args.showMessage) {
// For non-lifecycle commands, just show the message
if (validationResult.errors.length > 0) {
Expand Down
3 changes: 1 addition & 2 deletions packages/vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ import {
handleOfficeFeedback,
officeChatRequestHandler,
} from "./officeChat/handlers";
import { VsCodeUI, initVSCodeUI } from "./qm/vsc_ui";
import { initVSCodeUI } from "./qm/vsc_ui";
import { ExtTelemetry } from "./telemetry/extTelemetry";
import { TelemetryEvent, TelemetryTriggerFrom } from "./telemetry/extTelemetryEvents";
import accountTreeViewProviderInstance from "./treeview/account/accountTreeViewProvider";
Expand Down Expand Up @@ -207,7 +207,6 @@ export async function activate(context: vscode.ExtensionContext) {

initVSCodeUI(context);
initializeGlobalVariables(context);

loadLocalizedStrings();

const uriHandler = new UriHandler();
Expand Down
8 changes: 4 additions & 4 deletions packages/vscode-extension/src/globalVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ export function initializeGlobalVariables(ctx: vscode.ExtensionContext): void {
} else {
isSPFxProject = fs.existsSync(path.join(workspaceUri?.fsPath ?? "./", "SPFx"));
}

if (!diagnosticCollection) {
diagnosticCollection = vscode.languages.createDiagnosticCollection("teamstoolkit");
}
}

export function checkIsSPFx(directory: string): boolean {
Expand Down Expand Up @@ -92,3 +88,7 @@ export function setTools(toolsInstance: Tools) {
export function setCore(coreInstance: FxCore) {
core = coreInstance;
}

export function setDiagnosticCollection(collection: vscode.DiagnosticCollection) {
diagnosticCollection = collection;
}
76 changes: 12 additions & 64 deletions packages/vscode-extension/src/qm/vsc_ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import {
commands,
Diagnostic,
DiagnosticSeverity,
ExtensionContext,
extensions,
Uri,
Expand Down Expand Up @@ -38,8 +37,9 @@ import {
TelemetryEvent,
TelemetryProperty,
} from "../telemetry/extTelemetryEvents";
import { diagnosticCollection, workspaceUri } from "../globalVariables";
import path = require("path");
import { diagnosticCollection, setDiagnosticCollection } from "../globalVariables";
import { featureFlagManager } from "@microsoft/teamsfx-core";
import { FeatureFlags } from "@microsoft/teamsfx-core";

export class TTKLocalizer implements Localizer {
loadingOptionsPlaceholder(): string {
Expand Down Expand Up @@ -151,7 +151,15 @@ export class VsCodeUI extends VSCodeUI {
}

showDiagnosticInfo(diagnostics: IDiagnosticInfo[]): void {
diagnosticCollection.clear();
if (!featureFlagManager.getBooleanValue(FeatureFlags.ShowDiagnostics)) {
return;
}
if (!diagnosticCollection) {
const collection = languages.createDiagnosticCollection("teamstoolkit");
setDiagnosticCollection(collection);
} else {
diagnosticCollection.clear();
}
const diagnosticMap: Map<string, Diagnostic[]> = new Map();
for (const diagnostic of diagnostics) {
let diagnosticsOfFile = diagnosticMap.get(diagnostic.filePath);
Expand Down Expand Up @@ -179,66 +187,6 @@ export class VsCodeUI extends VSCodeUI {
diagnosticMap.forEach((diags, filePath) => {
diagnosticCollection.set(Uri.file(filePath), diags);
});

// diagnosticCollection.clear();
// const errors = [
// {
// id: "958d86ff-864b-474d-bea4-d8068b8c8cad",
// title: "ShortNameContainsPreprodWording",
// content: "Short name doesn't contain beta environment keywords",
// helpUrl:
// "https://docs.microsoft.com/en-us/microsoftteams/platform/resources/schema/manifest-schema#name",
// filePath: "manifest.json",
// shortCodeNumber: 4000,
// validationCategory: "Name",
// },
// ];

// const collection2 = languages.createDiagnosticCollection("teamstoolkit2");

// const diagnosticMap: Map<string, Diagnostic[]> = new Map();
// errors.forEach((error) => {
// const canonicalFile = "manifest.json";
// const regex = new RegExp(error.validationCategory);

// // const text = document.getText();

// // const line = document.lineAt(document.positionAt(matches.index).line);
// // const indexOf = line.text.indexOf(match);
// // const position = new Position(line.lineNumber, indexOf);
// const range = new Range(new Position(6, 2), new Position(6, 6));

// let diagnostics = diagnosticMap.get(canonicalFile);
// if (!diagnostics) {
// diagnostics = [];
// }

// //const message = `[✏️Edit env file](${commandUri.toString()})`;
// const diag = new Diagnostic(
// range,
// "Short name doesn't contain beta environment keywords",
// DiagnosticSeverity.Warning
// );
// diag.code = {
// value: "NameField",
// target: Uri.parse(
// "https://docs.microsoft.com/en-us/microsoftteams/platform/resources/schema/manifest-schema#name"
// ),
// };
// diag.source = "TTK";

// diagnostics.push(diag);
// diagnosticMap.set(canonicalFile, diagnostics);

// const fileUri = Uri.file(
// path.join(workspaceUri?.fsPath?.toString() ?? "", "appPackage", "manifest.json")
// );
// console.log(fileUri);
// diagnosticMap.forEach((diags, file) => {
// diagnosticCollection.set(fileUri, diags);
// collection2.set(fileUri, diags);
// });
// });
}
}

Expand Down
8 changes: 7 additions & 1 deletion packages/vscode-extension/test/mocks/vscode-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MockClipboard {
}

export function initialize() {
generateMock("languages");
//generateMock("languages");
generateMock("debug");
generateMock("scm");
generateNotebookMocks();
Expand Down Expand Up @@ -195,6 +195,12 @@ mockedVSCode.extensions = {
all: [],
};

(mockedVSCode as any).languages = {
createDiagnosticCollection: () => {},
registerCodeLensProvider: () => {},
registerHoverProvider: () => {},
};

// Setup commands APIs
mockedVSCode.commands = {
executeCommand: () => {
Expand Down
83 changes: 83 additions & 0 deletions packages/vscode-extension/test/qm/vsc_ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import * as sinon from "sinon";
import { stubInterface } from "ts-sinon";
import {
commands,
DiagnosticCollection,
Disposable,
ExtensionContext,
languages,
QuickInputButton,
QuickPick,
Terminal,
Expand All @@ -30,6 +32,8 @@ import { FxQuickPickItem, sleep, UserCancelError } from "@microsoft/vscode-ui";
import { VsCodeUI } from "../../src/qm/vsc_ui";
import { ExtTelemetry } from "../../src/telemetry/extTelemetry";
import { VsCodeLogProvider } from "../../src/commonlib/log";
import { featureFlagManager } from "@microsoft/teamsfx-core";
import * as globalVariables from "../../src/globalVariables";

describe("UI Unit Tests", async () => {
afterEach(() => {
Expand Down Expand Up @@ -939,4 +943,83 @@ describe("UI Unit Tests", async () => {
}
});
});

describe("showDiagnosticInfo", () => {
const sandbox = sinon.createSandbox();
let collection: DiagnosticCollection | undefined;

afterEach(() => {
sandbox.restore();
globalVariables.setDiagnosticCollection(undefined as unknown as DiagnosticCollection);
});

it("do nothing if feature flag is disabled", () => {
sandbox.stub(featureFlagManager, "getBooleanValue").returns(false);
const ui = new VsCodeUI(<ExtensionContext>{});
ui.showDiagnosticInfo([]);
});

it("show diagnostics first time if feature flag is enabled", () => {
const records: [string, { message: string }][] = [];
sandbox.stub(featureFlagManager, "getBooleanValue").returns(true);
collection = {
set: (filePath: string, diag: { message: string }) => {
records.push([filePath, diag]);
},
} as unknown as DiagnosticCollection;

sandbox.stub(languages, "createDiagnosticCollection").returns(collection as any);
const ui = new VsCodeUI(<ExtensionContext>{});

ui.showDiagnosticInfo([
{
startIndex: 0,
startLine: 1,
endIndex: 10,
endLine: 10,
severity: 2,
filePath: "test",
message: "error",
},
]);

expect(globalVariables.diagnosticCollection).not.undefined;
expect(records.length).equals(1);
});

it("show diagnostics not first time if feature flag is enabled", () => {
const records: [string, { message: string }][] = [];
sandbox.stub(featureFlagManager, "getBooleanValue").returns(true);
collection = {
clear: () => {
return;
},
set: (filePath: string, diag: { message: string }) => {
records.push([filePath, diag]);
},
} as unknown as DiagnosticCollection;

sandbox.stub(languages, "createDiagnosticCollection").returns(collection as any);
const ui = new VsCodeUI(<ExtensionContext>{});

ui.showDiagnosticInfo([
{
startIndex: 0,
startLine: 1,
endIndex: 10,
endLine: 10,
severity: 2,
filePath: "test",
message: "error",
code: {
value: "test",
link: "https://test.com",
},
},
]);

expect(globalVariables.diagnosticCollection).not.undefined;
expect(records.length).equals(1);
});
});
});

0 comments on commit 3c6e095

Please sign in to comment.