Skip to content

Commit 05dc7f7

Browse files
committed
visualpython#155 - Add 'Comment Template' to comment logic block.
add comment template function on comment logic block
1 parent 944b218 commit 05dc7f7

File tree

1 file changed

+110
-7
lines changed

1 file changed

+110
-7
lines changed

js/m_logic/Comment.js

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Note : Logic > comment
77
* License : GNU GPLv3 with Visual Python special exception
88
* Date : 2021. 11. 18
9-
* Change Date :
9+
* Change Date : 2022. 08. 02
1010
*/
1111

1212
//============================================================================
@@ -18,6 +18,67 @@ define([
1818
], function(com_String, PopupComponent) {
1919

2020
const COMMENT_DEFAULT_CODE = '# Write down comments'
21+
// Templates from NumPy Style Python Docstrings.
22+
const COMMENT_CLASS_TEMPLATE =
23+
`
24+
"""Summarize the class in one line.
25+
26+
Several sentences ...
27+
providing an extended description.
28+
29+
Note
30+
----------
31+
Note about this class.
32+
33+
Parameters
34+
----------
35+
param1 : param_type
36+
Parameter description.
37+
param2 : param_type
38+
Parameter description.
39+
40+
Attributes
41+
----------
42+
attr1 : attr_type
43+
Attibute description.
44+
attr2 : attr_type
45+
Attibute description.
46+
47+
Examples
48+
----------
49+
50+
References
51+
----------
52+
53+
"""
54+
`
55+
const COMMENT_METHOD_TEMPLATE =
56+
`
57+
"""Summarize the function in one line.
58+
59+
Several sentences ...
60+
providing an extended description.
61+
62+
Parameters
63+
----------
64+
param1 : param_type
65+
Parameter description.
66+
param2 : param_type
67+
Parameter description.
68+
69+
Returns
70+
-------
71+
return_type
72+
Return description.
73+
74+
Note
75+
----------
76+
77+
Examples
78+
----------
79+
80+
"""
81+
`
2182

2283
/**
2384
* Comment
@@ -29,45 +90,87 @@ define([
2990
this.config.dataview = false;
3091
this.config.codeview = false;
3192
this.config.saveOnly = true;
93+
94+
this.cmKey = 'code'; // Code Mirror Key
95+
this.selectBoxClassName = 'vp-ct-option'; // Select Box ClassName
3296

3397
this.state = {
3498
code: COMMENT_DEFAULT_CODE,
3599
...this.state
100+
};
101+
102+
this.cmTemplates = { // a kind of templates
103+
Template : COMMENT_DEFAULT_CODE,
104+
Class : COMMENT_CLASS_TEMPLATE,
105+
Method : COMMENT_METHOD_TEMPLATE
36106
}
37107

38-
this._addCodemirror('code', this.wrapSelector('#code'));
108+
this._addCodemirror(this.cmKey, this.wrapSelector('#code'));
39109
}
40110

41111
_bindEvent() {
42112
super._bindEvent();
43-
/** Implement binding events */
113+
114+
var commentTemplates = this.cmTemplates;
115+
var cm_key = this.cmKey;
116+
let cmCodeListTemp = this.cmCodeList;
117+
118+
// Select box change Event
119+
$('.' + this.selectBoxClassName).on('change', function(){
120+
// get code mirror object
121+
let targetCmObj = cmCodeListTemp.filter(obj => obj.key == cm_key);
122+
var templateOption = $(this).val();
123+
let cm = targetCmObj[0].cm;
124+
125+
// Change Code Mirror Text
126+
if(templateOption == 'vp_template_class'){
127+
cm.setValue(commentTemplates.Class);
128+
}else if(templateOption == 'vp_template_method'){
129+
cm.setValue(commentTemplates.Method);
130+
}else if(templateOption == 'vp_template_template'){
131+
cm.setValue(commentTemplates.Template);
132+
}
133+
134+
cm.save();
135+
});
136+
44137
}
45138

139+
46140
templateForBody() {
47141
/** Implement generating template */
48142
var page = new com_String();
49143
page.appendFormatLine('<textarea name="code" class="code vp-state" id="code">{0}</textarea>'
50144
, this.state.code);
145+
// add select box
146+
page.appendFormatLine('<select class="vp-select w100 {0}" >', this.selectBoxClassName);
147+
// add options
148+
Object.entries(this.cmTemplates).forEach(([opt, t_code]) => {
149+
page.appendFormatLine('<option value="{0}">{1}</option>',
150+
'vp_template_' + opt.toLowerCase(), opt);
151+
});
152+
page.appendFormatLine('</select>');
153+
51154
return page.toString();
52155
}
53156

54157
open() {
55158
super.open();
56159

57160
if (this.state.code === COMMENT_DEFAULT_CODE) {
58-
// set default selection
59-
let cmObj = this.getCodemirror('code');
161+
162+
let cmObj = this.getCodemirror(this.cmKey);
60163
if (cmObj && cmObj.cm) {
61164
cmObj.cm.setSelection({ line: 0, ch: 2 }, { line: 0 });
62165
cmObj.cm.focus();
63166
}
64167
}
65168
}
66-
169+
67170
generateCode() {
68171
return this.state.code;
69172
}
70-
173+
71174
}
72175

73176
return Comment;

0 commit comments

Comments
 (0)