Skip to content

Commit

Permalink
Merge pull request #11 from k--kato/wontfix/#9
Browse files Browse the repository at this point in the history
fixed #9 Activate on Enter
  • Loading branch information
Keisuke KATO authored Jun 24, 2016
2 parents c2f8ae1 + 67fc471 commit d055d9c
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 47 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ Type "///", it auto-generates an XML doucumentation comment like this:
![docomment](images/docomment.gif)


```csharp
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <returns></returns>
public int bb(string s, ref int y, void * z)
## Configuration

The menu under File > Preferences (Code > Preferences on Mac) provides entries to configure user and workspace settings. You are provided with a list of Default Settings. Copy any setting that you want to change to the related `settings.json` file.

### settings.json

```json
{
return 1;
// Press the Enter key to activate a command
"docomment.activateOnEnter": true
}
```


## Installation

1. Install Visual Studio Code 0.10.6 or higher
1. Install Visual Studio Code 1.2.0 or higher
1. Launch Code
1. From the command palette `Ctrl`-`Shift`-`P` (Windows, Linux) or `Cmd`-`Shift`-`P` (OSX)
1. Select `Install Extension`
Expand Down
27 changes: 19 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "docomment",
"version": "0.0.5",
"version": "0.0.6",
"publisher": "k--kato",
"engines": {
"vscode": "^0.10.x"
"vscode": "^1.2.x"
},
"displayName": "C# XML Documentation Comments",
"description": "Generate C# XML documentation comments for ///",
Expand All @@ -22,7 +22,18 @@
"language": "csharp",
"path": "./snippets/csharp.json"
}
]
],
"configuration": {
"type": "object",
"title": "C# XML Documentation Comments configuration",
"properties": {
"docomment.activateOnEnter": {
"type": "boolean",
"default": false,
"description": "Activating the Enter key."
}
}
}
},
"activationEvents": [
"onLanguage:csharp"
Expand All @@ -34,12 +45,12 @@
"dependencies": {
},
"devDependencies": {
"typescript": "^1.8.9",
"vscode": "^0.11.10",
"tslint": "^3.6.0",
"istanbul": "^0.4.2",
"typescript": "^1.8.10",
"vscode": "^0.11.13",
"tslint": "^3.11.0",
"istanbul": "^0.4.4",
"coveralls": "^2.11.9",
"mocha": "^2.4.5",
"mocha": "^2.5.3",
"mocha-lcov-reporter": "^1.2.0"
},
"extensionDependencies": [
Expand Down
24 changes: 22 additions & 2 deletions src/Controller/DocommentController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {window, workspace, Disposable, TextEditor, TextDocumentContentChangeEvent} from 'vscode';
import {window, workspace, Disposable, TextEditor, TextDocumentContentChangeEvent, WorkspaceConfiguration} from 'vscode';
import {IDocommentDomain} from '../Domain/IDocommentDomain';
import {IDocommentController} from './IDocommentController';
import {Configuration} from '../Entity/Config/Contributes/Configuration';

export class DocommentController implements IDocommentController {

Expand All @@ -17,13 +18,18 @@ export class DocommentController implements IDocommentController {
/* @implements */
public _docommentDomain: IDocommentDomain;

/* @implements */
public _config: Configuration;

/*-------------------------------------------------------------------------
* Entry Constructor
*-----------------------------------------------------------------------*/
public constructor(docommentDomain: IDocommentDomain) {
this._docommentDomain = docommentDomain;

/* Load Configuration File (.vscode/settings.json) */
this.loadConfig();

const subscriptions: Disposable[] = [];

/* Add Text Change Event */
Expand All @@ -34,6 +40,11 @@ export class DocommentController implements IDocommentController {
}
}, this, subscriptions);

/* Add Config File Change Event */
workspace.onDidChangeConfiguration(() => {
this.loadConfig();
}, this, subscriptions);

this._disposable = Disposable.from(...subscriptions);
}

Expand All @@ -47,13 +58,22 @@ export class DocommentController implements IDocommentController {
this._disposable.dispose();
}

/*-------------------------------------------------------------------------
* Private Method
*-----------------------------------------------------------------------*/
private loadConfig() {
const conf: WorkspaceConfiguration = workspace.getConfiguration(Configuration.KEY);

this._config = new Configuration();
this._config.activateOnEnter = conf.get<boolean>(Configuration.ACTIVATE_ON_ENTER, false);
}

/*-------------------------------------------------------------------------
* Event
*-----------------------------------------------------------------------*/
private _onEvent(activeEditor: TextEditor, event: TextDocumentContentChangeEvent) {
// Insert XML document comment
this._docommentDomain.Execute(activeEditor, event, this._languageId);
this._docommentDomain.Execute(activeEditor, event, this._languageId, this._config);
}

}
2 changes: 2 additions & 0 deletions src/Controller/IDocommentController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Disposable} from 'vscode';
import {IDocommentDomain} from '../Domain/IDocommentDomain';
import {Configuration} from '../Entity/Config/Contributes/Configuration';

export interface IDocommentController {

Expand All @@ -9,6 +10,7 @@ export interface IDocommentController {
_languageId: string;
_disposable: Disposable;
_docommentDomain: IDocommentDomain;
_config: Configuration;


/*-------------------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion src/Domain/DocommentDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {TextEditor, TextDocumentContentChangeEvent, Position} from 'vscode';
import {VSCodeApi} from '../Api/VSCodeApi';
import {IDocommentDomain, CodeType} from './IDocommentDomain';
import {StringUtil} from '../Utility/StringUtil';
import {Configuration} from '../Entity/Config/Contributes/Configuration';

export class DocommentDomain implements IDocommentDomain {

Expand All @@ -18,17 +19,23 @@ export class DocommentDomain implements IDocommentDomain {
/* @implements */
public _activeEditor: TextEditor;

/* @implements */
public _config: Configuration;

/*-------------------------------------------------------------------------
* Entry Method
*-----------------------------------------------------------------------*/

/* @implements */
public Execute(activeEditor: TextEditor, event: TextDocumentContentChangeEvent, languageId: string) {
public Execute(activeEditor: TextEditor
, event: TextDocumentContentChangeEvent
, languageId: string
, config: Configuration) {

this._event = event;
this._activeEditor = activeEditor;
this._vsCodeApi = new VSCodeApi(activeEditor);
this._config = config;

// Detect Language
if (!this._vsCodeApi.IsLanguage(languageId)) return;
Expand Down
8 changes: 6 additions & 2 deletions src/Domain/IDocommentDomain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {TextEditor, TextDocumentContentChangeEvent} from 'vscode';
import {VSCodeApi} from '../Api/VSCodeApi';
import {Configuration} from '../Entity/Config/Contributes/Configuration';

/*-------------------------------------------------------------------------
* Enum
Expand Down Expand Up @@ -27,12 +28,15 @@ export interface IDocommentDomain {
_event: TextDocumentContentChangeEvent;
_vsCodeApi: VSCodeApi;
_activeEditor: TextEditor;

_config: Configuration;

/*-------------------------------------------------------------------------
* Entry Method
*-----------------------------------------------------------------------*/
Execute(activeEditor: TextEditor, event: TextDocumentContentChangeEvent, languageId: string);
Execute(activeEditor: TextEditor
, event: TextDocumentContentChangeEvent
, languageId: string
, config: Configuration);


/*-------------------------------------------------------------------------
Expand Down
33 changes: 17 additions & 16 deletions src/Domain/Lang/DocommentDomainCSharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,38 @@ export class DocommentDomainCSharp extends DocommentDomain {
// NG: KeyCode is NOT '/' or Enter
const activeChar: string = this._vsCodeApi.ReadCharAtCurrent();
if (activeChar == null) return false;
const isSlashKey: boolean = (activeChar === '/');
const isEnterKey: boolean = (activeChar === '') && this._event.text.startsWith('\n');
const isSlashKey: boolean = SyntacticAnalysisCSharp.IsSlashKey(activeChar);
const isEnterKey: boolean = SyntacticAnalysisCSharp.IsEnterKey(activeChar, this._event.text);
if (!isSlashKey && !isEnterKey) return false;

// NG: Activate on Enter NOT Slash
if (this._config.activateOnEnter) {
if (isSlashKey) {
return false;
}
}

// NG: Line is NOT /// (NG: ////)
const activeLine: string = this._vsCodeApi.ReadLineAtCurrent();
if (activeLine == null) return false;

if (isSlashKey) {
const isDocComment: boolean = (activeLine.match(/(?:[^/]\/{3}$)|(?:^\/{3}[^/])|(?:^\/{3}$)/) !== null); // fixme: to simple
const isDocComment: boolean = SyntacticAnalysisCSharp.IsDocComment(activeLine);
if (!isDocComment) return false;
}
if (isEnterKey) {
const isDocComment: boolean = (activeLine.match(/\/{3}/) !== null);
const isDocComment: boolean = SyntacticAnalysisCSharp.IsDocComment(activeLine);
if (!isDocComment) return false;

const nextLine = this._vsCodeApi.ReadLine(this._vsCodeApi.GetActiveLine() + 2);
if (!nextLine.match(/\/{3}/)) return false;

return true;
}

// NG: Position is NOT ///
const position: number = this._vsCodeApi.GetActiveCharPosition();
const positionDocComment: number = activeLine.lastIndexOf('///') + ((isEnterKey) ? 3 : 2);
const isLastPosition: boolean = (position === positionDocComment);
if (!isLastPosition) return false;
// const position: number = this._vsCodeApi.GetActiveCharPosition();
// const positionDocComment: number = activeLine.lastIndexOf('///') + ((isEnterKey) ? 3 : 2);
// const isLastPosition: boolean = (position === positionDocComment);
// if (!isLastPosition) return false;

// NG: Previous line is XML document comment
const previousLine: string = this._vsCodeApi.ReadPreviousLineFromCurrent();
if (previousLine != null && previousLine.match(/\/{3}/)) return false;
// const previousLine: string = this._vsCodeApi.ReadPreviousLineFromCurrent();
// if (SyntacticAnalysisCSharp.IsDocComment(previousLine)) return false;

// OK
return true;
Expand Down
15 changes: 15 additions & 0 deletions src/Entity/Config/Contributes/Configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class Configuration {

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

public static ACTIVATE_ON_ENTER: string = "activateOnEnter";

/*-------------------------------------------------------------------------
* Field
*-----------------------------------------------------------------------*/
public activateOnEnter: Boolean;

}
16 changes: 15 additions & 1 deletion src/SyntacticAnalysis/SyntacticAnalysisCSharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@ export class SyntacticAnalysisCSharp {
private static RESERVED_WORDS: RegExp =
/(void|event|delegate|internal|public|protected|private|static|const|new|sealed|abstract|virtual|override|extern|unsafe|readonly|volatile|implicit|explicit|operator)/;

/*-------------------------------------------------------------------------
* Public Method: Comment Type
*-----------------------------------------------------------------------*/
public static IsEnterKey(activeChar: string, text: string): boolean {
return (activeChar === '') && text.startsWith('\n');
}

public static IsSlashKey(activeChar: string): boolean {
return (activeChar === '/');
}

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

/*-------------------------------------------------------------------------
* Public Method
* Public Method: Code Type
*-----------------------------------------------------------------------*/
public static IsNamespace(code: string): boolean {
if (code === null) return false;
Expand Down
3 changes: 3 additions & 0 deletions test/TestData/X.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace N // "N:N"
{
public unsafe class X // "T:N.X"
Expand All @@ -21,5 +23,6 @@ public class Nested{} // "T:N.X.Nested"
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);
}
}
6 changes: 0 additions & 6 deletions test/TestData/coverageconfig.json

This file was deleted.

20 changes: 20 additions & 0 deletions test/TestData/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": false
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-*"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dnxcore50",
"portable-net45+win8"
]
}
}
}

0 comments on commit d055d9c

Please sign in to comment.