Skip to content

Commit

Permalink
Merge pull request #12 from k--kato/wontfix/#10
Browse files Browse the repository at this point in the history
fixed #10 Support for tabs indentation
  • Loading branch information
Keisuke KATO authored Jun 24, 2016
2 parents d055d9c + 7de2346 commit 413ff1d
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 51 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ The menu under File > Preferences (Code > Preferences on Mac) provides entries t

```json
{
// Press the Enter key to activate a command
"docomment.activateOnEnter": true
// Press the Enter key to activate a command (Default: false)
"docomment.activateOnEnter": true,
// Insert spaces when pressing Tab.
"editor.insertSpaces": true,
// The number of spaces a tab is equal to.
"editor.tabSize": 4
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"docomment.activateOnEnter": {
"type": "boolean",
"default": false,
"description": "Activating the Enter key."
"description": "Press the Enter key to activate a command."
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Controller/DocommentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ export class DocommentController implements IDocommentController {
* Private Method
*-----------------------------------------------------------------------*/
private loadConfig() {
const conf: WorkspaceConfiguration = workspace.getConfiguration(Configuration.KEY);
const confDocomment: WorkspaceConfiguration = workspace.getConfiguration(Configuration.KEY_DOCOMMENT);
const confEditor: WorkspaceConfiguration = workspace.getConfiguration(Configuration.KEY_EDITOR);

this._config = new Configuration();
this._config.activateOnEnter = conf.get<boolean>(Configuration.ACTIVATE_ON_ENTER, false);
this._config.activateOnEnter = confDocomment.get<boolean>(Configuration.ACTIVATE_ON_ENTER, false);
this._config.insertSpaces = confEditor.get<boolean>(Configuration.INSERT_SPACES, false);
this._config.tabSize = confEditor.get<number>(Configuration.TAB_SIZE, 4);
}

/*-------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion src/Domain/DocommentDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,4 @@ export class DocommentDomain implements IDocommentDomain {
// NOP
}


}
19 changes: 12 additions & 7 deletions src/Domain/Lang/DocommentDomainCSharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ export class DocommentDomainCSharp extends DocommentDomain {
const activeLine: string = this._vsCodeApi.ReadLineAtCurrent();
if (activeLine == null) return false;
if (isSlashKey) {
const isDocComment: boolean = SyntacticAnalysisCSharp.IsDocComment(activeLine);
const isDocComment: boolean = SyntacticAnalysisCSharp.IsDocCommentStrict(activeLine);
if (!isDocComment) return false;

// NG: '/' => Insert => Event => ' /// '
if (SyntacticAnalysisCSharp.IsDoubleDocComment(activeLine)) return false;
}
if (isEnterKey) {
const isDocComment: boolean = SyntacticAnalysisCSharp.IsDocComment(activeLine);
const isDocComment: boolean = SyntacticAnalysisCSharp.IsDocComment(activeLine);
if (!isDocComment) return false;
}

Expand Down Expand Up @@ -151,8 +154,9 @@ export class DocommentDomainCSharp extends DocommentDomain {
const position: Position = this._vsCodeApi.GetActivePosition();

if (codeType === CodeType.Comment) {
const indentLen: number = StringUtil.GetIndent(code).length;
const insertPosition: Position = this._vsCodeApi.GetPosition(position.line + 1, indentLen);
const indent: string = StringUtil.GetIndent(code, this._config.insertSpaces, this._config.tabSize);
const indentLen: number = StringUtil.GetIndentLen(indent, this._config.insertSpaces, this._config.tabSize);
const insertPosition: Position = this._vsCodeApi.GetPosition(position.line + 1, indentLen - 1);
this._vsCodeApi.InsertText(insertPosition, docommnet);
} else {
const insertPosition: Position = this._vsCodeApi.ShiftPositionChar(position, 1);
Expand All @@ -165,8 +169,9 @@ export class DocommentDomainCSharp extends DocommentDomain {
const curPosition = this._vsCodeApi.GetActivePosition();

if (codeType === CodeType.Comment) {
const indentLen: number = StringUtil.GetIndent(code).length;
this._vsCodeApi.MoveSelection(curPosition.line + 1, indentLen + docomment.length);
const indent: string = StringUtil.GetIndent(code, this._config.insertSpaces, this._config.tabSize);
const indentLen: number = StringUtil.GetIndentLen(indent, this._config.insertSpaces, this._config.tabSize);
this._vsCodeApi.MoveSelection(curPosition.line + 1, indentLen - 1 + docomment.length);
} else {
this._vsCodeApi.MoveSelection(curPosition.line + 1, curPosition.character + 2);
}
Expand Down Expand Up @@ -199,7 +204,7 @@ export class DocommentDomainCSharp extends DocommentDomain {
}

// Format
const indent: string = StringUtil.GetIndent(code);
const indent: string = StringUtil.GetIndent(code, this._config.insertSpaces, this._config.tabSize);
let docomment = ' ' + docommentList[0] + '\n';
for (let i = 1; i < docommentList.length; i++) {
docomment += indent + '/// ' + docommentList[i];
Expand Down
16 changes: 11 additions & 5 deletions src/Entity/Config/Contributes/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
export class Configuration {

/*-------------------------------------------------------------------------
* Const
* docomment
*-----------------------------------------------------------------------*/
public static KEY: string = "docomment";

public static KEY_DOCOMMENT: string = "docomment";
public static ACTIVATE_ON_ENTER: string = "activateOnEnter";

public activateOnEnter: boolean;

/*-------------------------------------------------------------------------
* Field
* editor
*-----------------------------------------------------------------------*/
public activateOnEnter: Boolean;
public static KEY_EDITOR: string = "editor";
public static INSERT_SPACES: string = "insertSpaces";
public static TAB_SIZE: string = "tabSize";

public insertSpaces: boolean;
public tabSize: number;

}
12 changes: 10 additions & 2 deletions src/SyntacticAnalysis/SyntacticAnalysisCSharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ export class SyntacticAnalysisCSharp {
return (activeChar === '/');
}

public static IsDocComment(activeLine: string): boolean {
public static IsDocCommentStrict(activeLine: string): boolean {
return activeLine.match(/(?:[^/]\/{3}[ \t]*$)|(?:^\/{3}[^/])|(?:^\/{3}[ \t]*$)/) !== null; // fixme: to simple
}

public static IsDocComment(activeLine: string): boolean {
return activeLine.match(/\/{3}/) !== null;
}

public static IsDoubleDocComment(activeLine: string): boolean {
return activeLine.match(/^[ \t]+\/{3} $/) !== null;
}

/*-------------------------------------------------------------------------
* Public Method: Code Type
*-----------------------------------------------------------------------*/
Expand Down Expand Up @@ -76,7 +84,7 @@ export class SyntacticAnalysisCSharp {

public static IsComment(code: string): boolean {
if (code === null) return false;
return code.match(/[ ]+/) !== null;
return code.match(/[ \t]+/) !== null;
}

public static GetMethodParamNameList(code: string): Array<string> {
Expand Down
19 changes: 17 additions & 2 deletions src/Utility/StringUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,24 @@ export class StringUtil {
return line.replace(/\/\/.*/, '').replace(/\/\*.*\*\//, '');
}

public static GetIndent(line: string): string {
public static GetIndent(line: string, insertSpaces: boolean, tabSize: number): string {
if (line === null) return null;
return line.match(/([ \t]*)?/)[0];
const indent: string = line.match(/([ \t]*)?/)[0];
const spaces: string = ' '.repeat(tabSize);
if (insertSpaces) {
return indent.split('\t').join(spaces);
} else {
return indent.split(spaces).join('\t');
}
}

public static GetIndentLen(indent: string, insertSpaces: boolean, tabSize: number): number {
if (indent === null) return 0;
if (insertSpaces) {
return indent.split(' ').length;
} else {
return indent.split('\t').length * tabSize;
}
}

}
3 changes: 3 additions & 0 deletions test/Entity/Config/Contributes/Configuration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
suite('Entity.Config.Contributes.Configuration Tests', () => {
// NOP
});
45 changes: 45 additions & 0 deletions test/TestData/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processName": "<example>"
}
]
}
14 changes: 14 additions & 0 deletions test/TestData/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
46 changes: 23 additions & 23 deletions test/TestData/X.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
using System;

namespace N // "N:N"
{
public unsafe class X // "T:N.X"
{
public X(){} // "M:N.X.#ctor"
public X(int i){} // "M:N.X.#ctor(System.Int32)"
public string q; // "F:N.X.q"
public const double PI = 3.14; // "F:N.X.PI"
public int f(){return 1;} // "M:N.X.f"
public int bb(string s, ref int y, void * z){return 1;} // "M:N.X.bb(System.String,System.Int32@,=System.Void*)"
public int gg(short[] array1, int[,] array){return 0;} // "M:N.X.gg(System.Int16[], System.Int32[0:,0:])"
public static X operator+(X x, X xx){return x;} // "M:N.X.op_Addition(N.X,N.X)"
public int prop {get{return 1;} set{}} // "P:N.X.prop"
public event D d; // "E:N.X.d"
public int this[string s]{get{return 1;}} // "P:N.X.Item(System.String)"
public class Nested{} // "T:N.X.Nested"
public delegate void D(int i); // "T:N.X.D"
public static explicit operator int(X x){return 1;} // "M:N.X.op_Explicit(N.X)~System.Int32"
public DbSet<Director> Directors { get; set; }
public List<int> bb<int>(string s, ref List<int> y, void * z){return 1;} // "M:N.X.bb(System.String,System.Int32@,=System.Void*)"
int bb(string s, ref int y, void * z){return 1;} // "M:N.X.bb(System.String,System.Int32@,=System.Void*)"
int Generate(int level);
public void Save(string data, Action<AchievementSavedResponse> onComplete = null);
Func<string, bool> FirstClassFunction(Func<bool, int> func);
{
public unsafe class X // "T:N.X"
{
public X(){} // "M:N.X.#ctor"
public X(int i){} // "M:N.X.#ctor(System.Int32)"
public string q; // "F:N.X.q"
public const double PI = 3.14; // "F:N.X.PI"
public int f(){return 1;} // "M:N.X.f"
public int bb(string s, ref int y, void * z){return 1;} // "M:N.X.bb(System.String,System.Int32@,=System.Void*)"
public int gg(short[] array1, int[,] array){return 0;} // "M:N.X.gg(System.Int16[], System.Int32[0:,0:])"
public static X operator+(X x, X xx){return x;} // "M:N.X.op_Addition(N.X,N.X)"
public int prop {get{return 1;} set{}} // "P:N.X.prop"
public event D d; // "E:N.X.d"
public int this[string s]{get{return 1;}} // "P:N.X.Item(System.String)"
public class Nested{} // "T:N.X.Nested"
public delegate void D(int i); // "T:N.X.D"
public static explicit operator int(X x){return 1;} // "M:N.X.op_Explicit(N.X)~System.Int32"
public DbSet<Director> Directors { get; set; }
public List<int> bb<int>(string s, ref List<int> y, void * z){return 1;} // "M:N.X.bb(System.String,System.Int32@,=System.Void*)"
int bb(string s, ref int y, void * z){return 1;} // "M:N.X.bb(System.String,System.Int32@,=System.Void*)"
int Generate(int level);
public void Save(string data, Action<AchievementSavedResponse> onComplete = null);
Func<string, bool> FirstClassFunction(Func<bool, int> func);
}
}
30 changes: 24 additions & 6 deletions test/Utility/StringUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ suite('Utility.StringUtil.GetIndent Tests', () => {
const code: string = null;

// act
const actual: string = StringUtil.GetIndent(code);
const actual: string = StringUtil.GetIndent(code, true, 4);

// assert
assert.equal(actual, null);
Expand All @@ -336,7 +336,7 @@ suite('Utility.StringUtil.GetIndent Tests', () => {
const code = '';

// act
const actual: string = StringUtil.GetIndent(code);
const actual: string = StringUtil.GetIndent(code, true, 4);

// assert
assert.equal(actual, '');
Expand All @@ -354,7 +354,7 @@ suite('Utility.StringUtil.GetIndent Tests', () => {
const code = ' foo ';

// act
const actual: string = StringUtil.GetIndent(code);
const actual: string = StringUtil.GetIndent(code, true, 4);

// assert
assert.equal(actual, ' ');
Expand All @@ -366,16 +366,34 @@ suite('Utility.StringUtil.GetIndent Tests', () => {
Method : GetIndent
STATE : -
IN : ' foo '
OUT : ' '
OUT : ' '
`, () => {
// arrange
const code = ' foo ';

// act
const actual: string = StringUtil.GetIndent(code);
const actual: string = StringUtil.GetIndent(code, true, 4);

// assert
assert.equal(actual, ' ');
assert.equal(actual, ' ');
});

test(`
Category: Black-box testing
Class : Utility.StringUtil
Method : GetIndent
STATE : -
IN : ' foo '
OUT : ' '
`, () => {
// arrange
const code = ' foo ';

// act
const actual: string = StringUtil.GetIndent(code, false, 4);

// assert
assert.equal(actual, ' ');
});

});

0 comments on commit 413ff1d

Please sign in to comment.