ddoc template, add option to insert /++ +/ comments

This commit is contained in:
Basile Burg 2017-04-30 10:35:08 +02:00
parent 359f107213
commit 7eeb5dbe0a
No known key found for this signature in database
GPG Key ID: 1868039F415CB8CF
6 changed files with 49 additions and 32 deletions

View File

@ -11,9 +11,9 @@ import
* Finds the declaration at caretLine and write its ddoc template * Finds the declaration at caretLine and write its ddoc template
* in the standard output. * in the standard output.
*/ */
void getDdocTemplate(const(Module) mod, int caretLine) void getDdocTemplate(const(Module) mod, int caretLine, bool plusComment)
{ {
DDocTemplateGenerator dtg = construct!DDocTemplateGenerator(caretLine); DDocTemplateGenerator dtg = construct!DDocTemplateGenerator(caretLine, plusComment);
dtg.visit(mod); dtg.visit(mod);
} }
@ -23,24 +23,28 @@ final class DDocTemplateGenerator: ASTVisitor
private: private:
int _caretline; immutable int _caretline;
immutable char c1;
immutable char[2] c2;
public: public:
this(int caretline) this(int caretline, bool plusComment)
{ {
_caretline = caretline; _caretline = caretline;
c1 = plusComment ? '+' : '*';
c2 = plusComment ? "++" : "**";
} }
override void visit(const(FunctionDeclaration) decl) override void visit(const(FunctionDeclaration) decl)
{ {
if (decl.name.line == _caretline) if (decl.name.line == _caretline)
{ {
writeln("/**\n * <short description> \n * \n * <detailed description>\n *"); writeln("/", c2, "\n ", c1, " <short description> \n ", c1, " \n ", c1, " <detailed description>\n ", c1);
if (decl.templateParameters || decl.parameters) if (decl.templateParameters || decl.parameters)
{ {
writeln(" * Params:"); writeln(" ", c1, " Params:");
if (decl.templateParameters && decl.templateParameters.templateParameterList) if (decl.templateParameters && decl.templateParameters.templateParameterList)
{ {
@ -48,13 +52,13 @@ public:
.templateParameterList.items) .templateParameterList.items)
{ {
if (p.templateAliasParameter) if (p.templateAliasParameter)
writeln(" * ", p.templateAliasParameter.identifier.text, " = <description>"); writeln(" ", c1, " ", p.templateAliasParameter.identifier.text, " = <description>");
else if (p.templateTupleParameter) else if (p.templateTupleParameter)
writeln(" * ", p.templateTupleParameter.identifier.text, " = <description>"); writeln(" ", c1, " ", p.templateTupleParameter.identifier.text, " = <description>");
else if (p.templateTypeParameter) else if (p.templateTypeParameter)
writeln(" * ", p.templateTypeParameter.identifier.text, " = <description>"); writeln(" ", c1, " ", p.templateTypeParameter.identifier.text, " = <description>");
else if (p.templateValueParameter) else if (p.templateValueParameter)
writeln(" * ", p.templateValueParameter.identifier.text, " = <description>"); writeln(" ", c1, " ", p.templateValueParameter.identifier.text, " = <description>");
} }
} }
if (decl.parameters) if (decl.parameters)
@ -62,9 +66,9 @@ public:
foreach(i, const Parameter p; decl.parameters.parameters) foreach(i, const Parameter p; decl.parameters.parameters)
{ {
if (p.name.text != "") if (p.name.text != "")
writeln(" * ", p.name.text, " = <description>"); writeln(" ", c1, " ", p.name.text, " = <description>");
else else
writeln(" * __param", i, " = <description>"); writeln(" ",c1, " __param", i, " = <description>");
} }
} }
} }
@ -73,10 +77,10 @@ public:
{ {
if (decl.returnType.type2 && decl.returnType.type2 if (decl.returnType.type2 && decl.returnType.type2
&& decl.returnType.type2.builtinType != tok!"void") && decl.returnType.type2.builtinType != tok!"void")
writeln(" * \n * Returns: <return description>"); writeln(" ", c1, " \n ", c1, " Returns: <return description>");
} }
writeln(" */"); writeln(" ", c1, "/");
} }
else if (decl.name.line > _caretline) else if (decl.name.line > _caretline)
@ -108,11 +112,11 @@ public:
{ {
if (decl.name.line == _caretline) if (decl.name.line == _caretline)
{ {
writeln("/**\n * <short description> \n * \n * <detailed description>\n *"); writeln("/", c2, "\n ", c1, " <short description> \n ", c1, " \n ", c1, " <detailed description>\n ", c1);
if (decl.templateParameters) if (decl.templateParameters)
{ {
writeln(" * Params:"); writeln(" ", c1, " Params:");
if (decl.templateParameters && decl.templateParameters.templateParameterList) if (decl.templateParameters && decl.templateParameters.templateParameterList)
{ {
@ -120,18 +124,18 @@ public:
.templateParameterList.items) .templateParameterList.items)
{ {
if (p.templateAliasParameter) if (p.templateAliasParameter)
writeln(" * ", p.templateAliasParameter.identifier.text, " = <description>"); writeln(" ", c1, " ", p.templateAliasParameter.identifier.text, " = <description>");
else if (p.templateTupleParameter) else if (p.templateTupleParameter)
writeln(" * ", p.templateTupleParameter.identifier.text, " = <description>"); writeln(" ", c1, " ", p.templateTupleParameter.identifier.text, " = <description>");
else if (p.templateTypeParameter) else if (p.templateTypeParameter)
writeln(" * ", p.templateTypeParameter.identifier.text, " = <description>"); writeln(" ", c1, " ", p.templateTypeParameter.identifier.text, " = <description>");
else if (p.templateValueParameter) else if (p.templateValueParameter)
writeln(" * ", p.templateValueParameter.identifier.text, " = <description>"); writeln(" ", c1, " ", p.templateValueParameter.identifier.text, " = <description>");
} }
} }
} }
writeln(" */"); writeln(" ", c1, "/");
} }
else if (decl.name.line > _caretline) else if (decl.name.line > _caretline)
@ -142,7 +146,7 @@ public:
version(unittest) version(unittest)
{ {
DDocTemplateGenerator parseAndVisit(const(char)[] source, int caretLine) DDocTemplateGenerator parseAndVisit(const(char)[] source, int caretLine, bool p = false)
{ {
writeln; writeln;
RollbackAllocator allocator; RollbackAllocator allocator;
@ -150,7 +154,7 @@ version(unittest)
StringCache cache = StringCache(StringCache.defaultBucketCount); StringCache cache = StringCache(StringCache.defaultBucketCount);
const(Token)[] tokens = getTokensForParser(cast(ubyte[]) source, config, &cache); const(Token)[] tokens = getTokensForParser(cast(ubyte[]) source, config, &cache);
Module mod = parseModule(tokens, "", &allocator); Module mod = parseModule(tokens, "", &allocator);
DDocTemplateGenerator result = construct!(DDocTemplateGenerator)(caretLine); DDocTemplateGenerator result = construct!(DDocTemplateGenerator)(caretLine, p);
result.visit(mod); result.visit(mod);
return result; return result;
} }
@ -160,7 +164,7 @@ unittest
{ {
q{ module a; q{ module a;
void foo(A...)(A a){} void foo(A...)(A a){}
}.parseAndVisit(2); }.parseAndVisit(2, true);
} }
unittest unittest
@ -174,7 +178,7 @@ unittest
{ {
q{ module a; q{ module a;
int foo(int){} int foo(int){}
}.parseAndVisit(2); }.parseAndVisit(2, true);
} }
unittest unittest

View File

@ -13,11 +13,16 @@ import
private __gshared int caretLine; private __gshared int caretLine;
private __gshared bool deepSymList; private __gshared bool option1;
private __gshared static Appender!(ubyte[]) source; private __gshared static Appender!(ubyte[]) source;
private __gshared static Appender!(AstErrors) errors; private __gshared static Appender!(AstErrors) errors;
private __gshared string[] files; private __gshared string[] files;
// -o : deep visit the symbols
alias deepSymList = option1;
// -o : outputs /++ +/ ddoc instead of /** */
alias plusComment = option1;
static this() static this()
{ {
GC.disable; GC.disable;
@ -51,7 +56,7 @@ void main(string[] args)
// options for the work // options for the work
getopt(args, std.getopt.config.passThrough, getopt(args, std.getopt.config.passThrough,
"d", &deepSymList, "o", &option1,
"l", &caretLine "l", &caretLine
); );
@ -152,7 +157,7 @@ void handleDdocTemplateOption()
source.data source.data
.getTokensForParser(config, &cache) .getTokensForParser(config, &cache)
.parseModule("", &alloc, &ignoreErrors) .parseModule("", &alloc, &ignoreErrors)
.getDdocTemplate(caretLine); .getDdocTemplate(caretLine, plusComment);
} }
private void handleErrors(string fname, size_t line, size_t col, string message, private void handleErrors(string fname, size_t line, size_t col, string message,

View File

@ -22,7 +22,7 @@ procedure getModulesImports(const files: string; results: TStrings);
procedure getHalsteadMetrics(source: TStrings; out jsn: TJSONObject); procedure getHalsteadMetrics(source: TStrings; out jsn: TJSONObject);
procedure getDdocTemplate(source, res: TStrings;caretLine: integer); procedure getDdocTemplate(source, res: TStrings;caretLine: integer; plusComment: boolean);
implementation implementation
@ -127,7 +127,7 @@ begin
end; end;
end; end;
procedure getDdocTemplate(source, res: TStrings; caretLine: integer); procedure getDdocTemplate(source, res: TStrings; caretLine: integer; plusComment: boolean);
var var
prc: TProcess; prc: TProcess;
str: string; str: string;
@ -140,6 +140,8 @@ begin
prc.Executable := str; prc.Executable := str;
prc.Parameters.Add('-K'); prc.Parameters.Add('-K');
prc.Parameters.Add('-l' + caretLine.ToString); prc.Parameters.Add('-l' + caretLine.ToString);
if plusComment then
prc.Parameters.Add('-o');
prc.Options := [poUsePipes {$IFDEF WINDOWS}, poNewConsole{$ENDIF}]; prc.Options := [poUsePipes {$IFDEF WINDOWS}, poNewConsole{$ENDIF}];
prc.ShowWindow := swoHIDE; prc.ShowWindow := swoHIDE;
prc.Execute; prc.Execute;

View File

@ -63,6 +63,7 @@ type
fAlwaysAdvancedFeatures: boolean; fAlwaysAdvancedFeatures: boolean;
fAutoClosedPairs: TAutoClosePairs; fAutoClosedPairs: TAutoClosePairs;
fSmartDdocNewline: boolean; fSmartDdocNewline: boolean;
fInsertPlusDdoc: boolean;
// //
procedure setPhobosDocRoot(value: TCEPathname); procedure setPhobosDocRoot(value: TCEPathname);
procedure setFont(value: TFont); procedure setFont(value: TFont);
@ -107,6 +108,7 @@ type
property options1: TSynEditorOptions read fOptions1 write fOptions1; property options1: TSynEditorOptions read fOptions1 write fOptions1;
property options2: TSynEditorOptions2 read fOptions2 write fOptions2; property options2: TSynEditorOptions2 read fOptions2 write fOptions2;
property phobosDocRoot: TCEPathname read fPhobosDocRoot write setPhobosDocRoot; property phobosDocRoot: TCEPathname read fPhobosDocRoot write setPhobosDocRoot;
property plusDdoc: boolean read fInsertPlusDdoc write fInsertPlusDdoc;
property resetFontSize: boolean read fResetFontSize write fResetFontSize default true; property resetFontSize: boolean read fResetFontSize write fResetFontSize default true;
property rightEdge: Integer read fRightEdge write fRightEdge default 80; property rightEdge: Integer read fRightEdge write fRightEdge default 80;
property rightEdgeColor: TColor read fRightEdgeColor write fRightEdgeColor default clSilver; property rightEdgeColor: TColor read fRightEdgeColor write fRightEdgeColor default clSilver;
@ -304,6 +306,7 @@ begin
identifierMatchOptions:=srcopt.identifierMatchOptions; identifierMatchOptions:=srcopt.identifierMatchOptions;
detectIndentMode:=srcopt.detectIndentMode; detectIndentMode:=srcopt.detectIndentMode;
fPhobosDocRoot:=srcopt.fPhobosDocRoot; fPhobosDocRoot:=srcopt.fPhobosDocRoot;
fInsertPlusDdoc:= srcopt.fInsertPlusDdoc;
fSmartDdocNewline:=srcopt.fSmartDdocNewline; fSmartDdocNewline:=srcopt.fSmartDdocNewline;
if fSmartDdocNewline then if fSmartDdocNewline then
@ -674,6 +677,7 @@ begin
anEditor.phobosDocRoot:=fPhobosDocRoot; anEditor.phobosDocRoot:=fPhobosDocRoot;
anEditor.alwaysAdvancedFeatures:=fAlwaysAdvancedFeatures; anEditor.alwaysAdvancedFeatures:=fAlwaysAdvancedFeatures;
anEditor.smartDdocNewline:= fSmartDdocNewline; anEditor.smartDdocNewline:= fSmartDdocNewline;
anEditor.insertPlusDdoc:= fInsertPlusDdoc;
for i := 0 to anEditor.Keystrokes.Count-1 do for i := 0 to anEditor.Keystrokes.Count-1 do
begin begin
kst := anEditor.Keystrokes.Items[i]; kst := anEditor.Keystrokes.Items[i];

View File

@ -685,7 +685,7 @@ begin
fToolProc.OnTerminate := @toolTerminated; fToolProc.OnTerminate := @toolTerminated;
fToolProc.CurrentDirectory := Application.ExeName.extractFileDir; fToolProc.CurrentDirectory := Application.ExeName.extractFileDir;
if fDeep then if fDeep then
fToolProc.Parameters.Add('-d'); fToolProc.Parameters.Add('-o');
fToolProc.Parameters.Add('-s'); fToolProc.Parameters.Add('-s');
fToolProc.Execute; fToolProc.Execute;
str := fDoc.Text; str := fDoc.Text;

View File

@ -181,6 +181,7 @@ type
fHasModuleDeclaration: boolean; fHasModuleDeclaration: boolean;
fLastCompletion: string; fLastCompletion: string;
fDebugger: ICEDebugger; fDebugger: ICEDebugger;
fInsertPlusDdoc: boolean;
procedure decCallTipsLvl; procedure decCallTipsLvl;
procedure setMatchOpts(value: TIdentifierMatchOptions); procedure setMatchOpts(value: TIdentifierMatchOptions);
function getMouseBytePosition: Integer; function getMouseBytePosition: Integer;
@ -305,6 +306,7 @@ type
property autoCloseCurlyBrace: TBraceAutoCloseStyle read fAutoCloseCurlyBrace write fAutoCloseCurlyBrace; property autoCloseCurlyBrace: TBraceAutoCloseStyle read fAutoCloseCurlyBrace write fAutoCloseCurlyBrace;
property autoClosedPairs: TAutoClosePairs read fAutoClosedPairs write fAutoClosedPairs; property autoClosedPairs: TAutoClosePairs read fAutoClosedPairs write fAutoClosedPairs;
property smartDdocNewline: boolean read fSmartDdocNewline write fSmartDdocNewline; property smartDdocNewline: boolean read fSmartDdocNewline write fSmartDdocNewline;
property insertPlusDdoc: boolean read fInsertPlusDdoc write fInsertPlusDdoc;
end; end;
TSortDialog = class(TForm) TSortDialog = class(TForm)
@ -1872,7 +1874,7 @@ var
begin begin
d := TStringList.Create; d := TStringList.Create;
try try
getDdocTemplate(lines, d, CaretY); getDdocTemplate(lines, d, CaretY, fInsertPlusDdoc);
if d.Text.isNotEmpty then if d.Text.isNotEmpty then
begin begin
BeginUndoBlock; BeginUndoBlock;