Skip to content

Commit

Permalink
Merge pull request #15 from k--kato/fix/enterKey
Browse files Browse the repository at this point in the history
replace blank line when activate on enter key
  • Loading branch information
Keisuke KATO authored Jul 11, 2016
2 parents acf62e4 + 075f829 commit cc2f192
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/Api/VSCodeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export class VSCodeApi {
return new Selection(line, charcter, line, charcter);
}

public GetSelectionByPosition(anchor: Position, active: Position): Selection {
return new Selection(anchor, active);
}

public MoveSelection(line: number, charcter: number): void {
const move: Selection = this.GetSelection(line, charcter);
this._activeEditor.selection = move;
Expand All @@ -67,6 +71,12 @@ export class VSCodeApi {
});
}

public ReplaceText(selection: Selection, text: string) {
this._activeEditor.edit((editBuilder) => {
editBuilder.replace(selection, text);
});
}

public ReadLine(line: number): string {
return this._activeEditor.document.lineAt(line).text;
}
Expand Down
30 changes: 21 additions & 9 deletions src/Domain/Lang/DocommentDomainCSharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {Position} from 'vscode';

export class DocommentDomainCSharp extends DocommentDomain {

/*-------------------------------------------------------------------------
* Field
*-----------------------------------------------------------------------*/
private _isEnterKey: boolean = false;


/*-------------------------------------------------------------------------
* Domain Method
Expand All @@ -18,8 +23,8 @@ export class DocommentDomainCSharp extends DocommentDomain {
const activeChar: string = this._vsCodeApi.ReadCharAtCurrent();
if (activeChar == null) return false;
const isSlashKey: boolean = SyntacticAnalysisCSharp.IsSlashKey(activeChar);
const isEnterKey: boolean = SyntacticAnalysisCSharp.IsEnterKey(activeChar, this._event.text);
if (!isSlashKey && !isEnterKey) return false;
this._isEnterKey = SyntacticAnalysisCSharp.IsEnterKey(activeChar, this._event.text);
if (!isSlashKey && !this._isEnterKey) return false;

// NG: Activate on Enter NOT Slash
if (this._config.activateOnEnter) {
Expand All @@ -38,7 +43,7 @@ export class DocommentDomainCSharp extends DocommentDomain {
// NG: '/' => Insert => Event => ' /// '
if (SyntacticAnalysisCSharp.IsDoubleDocComment(activeLine)) return false;
}
if (isEnterKey) {
if (this._isEnterKey) {
const isDocComment: boolean = SyntacticAnalysisCSharp.IsDocComment(activeLine);
if (!isDocComment) return false;
}
Expand Down Expand Up @@ -150,28 +155,35 @@ export class DocommentDomainCSharp extends DocommentDomain {
}

/* @implements */
public WriteDocomment(code: string, codeType: CodeType, docommnet: string): void {
public WriteDocomment(code: string, codeType: CodeType, docomment: string): void {
const position: Position = this._vsCodeApi.GetActivePosition();

if (codeType === CodeType.Comment) {
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);
this._vsCodeApi.InsertText(insertPosition, docomment);
} else {
const insertPosition: Position = this._vsCodeApi.ShiftPositionChar(position, 1);
this._vsCodeApi.InsertText(insertPosition, docommnet);
if (this._isEnterKey) {
const active: Position = this._vsCodeApi.GetActivePosition();
const anchor: Position = this._vsCodeApi.GetPosition(active.line + 1, active.character);
const replaceSelection = this._vsCodeApi.GetSelectionByPosition(anchor, active);
this._vsCodeApi.ReplaceText(replaceSelection, docomment);
} else {
const insertPosition: Position = this._vsCodeApi.ShiftPositionChar(position, 1)
this._vsCodeApi.InsertText(insertPosition, docomment);
}
}
}

/* @implements */
public MoveCursorTo(code: string, codeType: CodeType, docomment: string): void {
const curPosition = this._vsCodeApi.GetActivePosition();

if (codeType === CodeType.Comment) {
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);
this._vsCodeApi.MoveSelection(curPosition.line + 1, indentLen - 1 + docomment.length);
} else {
this._vsCodeApi.MoveSelection(curPosition.line + 1, curPosition.character + 2);
}
Expand Down

0 comments on commit cc2f192

Please sign in to comment.