Skip to content

Commit

Permalink
Add feature to src folder of settings #61
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidFeldhoff committed Oct 31, 2020
1 parent b7a421b commit 8eaa852
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
5 changes: 4 additions & 1 deletion vscode-extension/src/App logic/Utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ export class Config {
static getPrefixThen(uri?: Uri): string {
return this.getConfig(uri).get<string>('prefixThen', '');
}
static getRemovalMode(uri?: Uri): string{
static getRemovalMode(uri?: Uri): string {
return this.getConfig(uri).get<string>('removalMode', '');
}
static getTestSrcFolder(): string | undefined {
return this.getConfig().get<string>('testDirectory');
}
private static getConfig(uri?: Uri): WorkspaceConfiguration {
return workspace.getConfiguration(this.app, uri);
}
Expand Down
24 changes: 18 additions & 6 deletions vscode-extension/src/App logic/Utils/elementInsertionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
import { writeFileSync } from "fs-extra";
import { Position, Range, TextDocument, workspace, WorkspaceEdit } from "vscode";
import { join } from "path";
import { Position, Range, TextDocument, workspace, WorkspaceEdit, WorkspaceFolder } from "vscode";
import { MessageUpdate, TypeChanged } from "../../typings/types";
import { ALFullSyntaxTreeNodeExt } from "../AL Code Outline Ext/alFullSyntaxTreeNodeExt";
import { FullSyntaxTreeNodeKind } from "../AL Code Outline Ext/fullSyntaxTreeNodeKind";
import { TextRangeExt } from "../AL Code Outline Ext/textRangeExt";
import { ALFullSyntaxTreeNode } from "../AL Code Outline/alFullSyntaxTreeNode";
import { SyntaxTree } from "../AL Code Outline/syntaxTree";
import { Config } from "./config";
import { ElementUtils } from "./elementUtils";
import { RangeUtils } from "./rangeUtils";
import { StringUtils } from "./stringUtils";
import { TestCodeunitUtils } from "./testCodeunitUtils";
import { TestMethodUtils } from "./testMethodUtils";
import { WorkspaceUtils } from "./workspaceUtils";

export class ElementInsertionUtils {
public static async addSomethingNewToCode(msg: MessageUpdate): Promise<boolean> {
if (msg.Type == TypeChanged.Feature) {
writeFileSync(msg.FsPath, TestCodeunitUtils.getDefaultTestCodeunit(msg.NewValue), { encoding: 'utf8' });
return true;
return ElementInsertionUtils.addNewFeatureToCode(msg);
} else if (msg.Type == TypeChanged.ScenarioName) {
//TODO: Get the workspacefolder using the msg.Project, search for the feature and add the scenario there.
//msg.Id will contain the next ID of the Feature
return await ElementInsertionUtils.addNewScenarioToCode(msg);
}
else {
return await ElementInsertionUtils.addNewElementToCode(msg);
}
}

private static addNewFeatureToCode(msg: MessageUpdate): boolean {
let srcFolder: string | undefined = Config.getTestSrcFolder();
if (!srcFolder)
throw new Error('Please specify the source folder for your tests in the settings.');
let fileName: string = new StringUtils(msg.NewValue).titleCase().removeSpecialChars().value() + '.al';
let fsPath: string = WorkspaceUtils.getFullFsPathOfRelativePath(srcFolder, fileName);
writeFileSync(fsPath, TestCodeunitUtils.getDefaultTestCodeunit(msg.NewValue), { encoding: 'utf8' });
return true;
}

static async addNewElementToCode(msg: MessageUpdate): Promise<boolean> {
if (msg.FsPath == '' && msg.Feature !== '' && msg.Project !== '')
msg.FsPath = await ElementUtils.getFSPathOfFeature(msg.Project, msg.Feature);
Expand Down Expand Up @@ -130,7 +142,7 @@ export class ElementInsertionUtils {
}
return { addEmptyLine: true, endPositionOfPreviousLine: scenarioRange.end };
}

public static addElement(edit: WorkspaceEdit, document: TextDocument, positionToInsert: Position, type: TypeChanged, elementValue: string) {
let textToAdd: string = '\r\n';
textToAdd += ' ' + ElementUtils.getElementComment(type, elementValue);
Expand Down
19 changes: 19 additions & 0 deletions vscode-extension/src/App logic/Utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export class StringUtils {
text: string;
constructor(text: string){
this.text = text;
}
public titleCase(): StringUtils {
this.text = this.text.replace(/\w+/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
return this;
}
public removeSpecialChars(): StringUtils {
this.text = this.text.replace(/[^\w]/g, '');
return this;
}
public value(){
return this.text;
}
}
6 changes: 2 additions & 4 deletions vscode-extension/src/App logic/Utils/testMethodUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import { ALFullSyntaxTreeNode } from "../AL Code Outline/alFullSyntaxTreeNode";
import { SyntaxTree } from "../AL Code Outline/syntaxTree";
import { Config } from "./config";
import { RangeUtils } from "./rangeUtils";
import { StringUtils } from "./stringUtils";

export class TestMethodUtils {
public static getProcedureName(type: TypeChanged, name: string): string {
let nameTitleCase: string = name.replace(/\w+/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
nameTitleCase = nameTitleCase.replace(/[^\w]/g, '');
let nameTitleCase: string = new StringUtils(name).titleCase().removeSpecialChars().value();

let prefix: string = '';
let uri: Uri | undefined = window.activeTextEditor?.document.uri;
Expand Down
20 changes: 20 additions & 0 deletions vscode-extension/src/App logic/Utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { join } from "path";
import { workspace, WorkspaceFolder } from "vscode";

export class WorkspaceUtils {
public static getFullFsPathOfRelativePath(relativeFolder: string, fileName: string) {
let relativeFsPath: string = join(relativeFolder, fileName);
let fullFsPath: string;
if (!workspace.workspaceFolders)
throw new Error('No workspacefolder opened.');
if (workspace.workspaceFile) {
fullFsPath = join(workspace.workspaceFile.fsPath, relativeFsPath);
} else if (workspace.workspaceFolders.length == 1) {
let workspaceFolder: WorkspaceFolder = workspace.workspaceFolders[0];
fullFsPath = join(workspaceFolder.uri.fsPath, relativeFsPath);
} else {
throw new Error('Expected to find the workspacefolder.')
}
return fullFsPath;
}
}

0 comments on commit 8eaa852

Please sign in to comment.