Skip to content

Commit

Permalink
Merge pull request #5109 from mkslanc/doc-comments-behaviours
Browse files Browse the repository at this point in the history
Doc comments highlight improvements + closing auto-insertion
  • Loading branch information
nightwing authored May 11, 2023
2 parents 204aafa + 536939d commit 4fba678
Show file tree
Hide file tree
Showing 43 changed files with 345 additions and 181 deletions.
5 changes: 5 additions & 0 deletions demo/kitchen-sink/docs/javascript.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
*
* @param {string[]} items
* @param nada
*/
function foo(items, nada) {
for (var i=0; i<items.length; i++) {
alert(items[i] + "juhu\n");
Expand Down
3 changes: 1 addition & 2 deletions src/mode/apex.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ var oop = require("../lib/oop");
var TextMode = require("../mode/text").Mode;
var ApexHighlightRules = require("./apex_highlight_rules").ApexHighlightRules;
var FoldMode = require("../mode/folding/cstyle").FoldMode;
var CstyleBehaviour = require("../mode/behaviour/cstyle").CstyleBehaviour;

function ApexMode() {
TextMode.call(this);

this.HighlightRules = ApexHighlightRules;
this.foldingRules = new FoldMode();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = this.$defaultBehaviour;
}

oop.inherits(ApexMode, TextMode);
Expand Down
26 changes: 13 additions & 13 deletions src/mode/asl.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"use strict";

var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var ASLHighlightRules = require("./asl_highlight_rules").ASLHighlightRules;
var FoldMode = require("./folding/cstyle").FoldMode;
var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var ASLHighlightRules = require("./asl_highlight_rules").ASLHighlightRules;
var FoldMode = require("./folding/cstyle").FoldMode;

var Mode = function () {
this.HighlightRules = ASLHighlightRules;
this.foldingRules = new FoldMode();
var Mode = function () {
this.HighlightRules = ASLHighlightRules;
this.foldingRules = new FoldMode();
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(Mode, TextMode);
};
oop.inherits(Mode, TextMode);

(function () {
this.$id = "ace/mode/asl";
}).call(Mode.prototype);
(function () {
this.$id = "ace/mode/asl";
}).call(Mode.prototype);

exports.Mode = Mode;
exports.Mode = Mode;
20 changes: 20 additions & 0 deletions src/mode/behaviour/behaviour_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,26 @@ module.exports = {
editor.setValue("p", 1);
exec("insertstring", 1, '"');
assert.equal(editor.getValue(), 'p"');
},
"test: doc comment auto-closing": function() {
editor.session.setMode(new JavaScriptMode());
editor.setWrapBehavioursEnabled(true);

// Test case 1: Starting a new doc comment
editor.setValue("/**", 1);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), "/**\n * \n */");

// Test case 2: Continuing an existing doc comment with asterisk on a new line
editor.setValue("/**\n * Test", 1);
editor.gotoLine(1, 5);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), "/**\n * \n * Test");

// Test case 3: Starting a new doc comment with 4-space indentation
editor.setValue(" /**", 1);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), " /**\n * \n */");
}
};

Expand Down
35 changes: 34 additions & 1 deletion src/mode/behaviour/cstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ var getWrapped = function(selection, selected, opening, closing) {
* @constructor
* @param {Object} [options] - The options for the Cstyle behaviour object.
* @param {boolean} [options.braces] - Whether to force braces auto-pairing.
* @param {boolean} [options.closeDocComment] - enables automatic insertion of closing tags for documentation comments.
*/
var CstyleBehaviour = function(options) {
options = options || {};
this.add("braces", "insertion", function(state, action, editor, session, text) {
var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row);
Expand All @@ -63,7 +65,7 @@ var CstyleBehaviour = function(options) {
if (selected !== "" && selected !== "{" && editor.getWrapBehavioursEnabled()) {
return getWrapped(selection, selected, '{', '}');
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
if (/[\]\}\)]/.test(line[cursor.column]) || editor.inMultiSelectMode || options && options.braces) {
if (/[\]\}\)]/.test(line[cursor.column]) || editor.inMultiSelectMode || options.braces) {
CstyleBehaviour.recordAutoInsert(editor, session, "}");
return {
text: '{}',
Expand Down Expand Up @@ -301,7 +303,38 @@ var CstyleBehaviour = function(options) {
}
}
});

if (options.closeDocComment !== false) {
this.add("doc comment end", "insertion", function (state, action, editor, session, text) {
if (state === "doc-start" && (text === "\n" || text === "\r\n") && editor.selection.isEmpty()) {
var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row);
var nextLine = session.doc.getLine(cursor.row + 1);
var indent = this.$getIndent(line);
if (/\s*\*/.test(nextLine)) {
if (/^\s*\*/.test(line)) {
return {
text: text + indent + "* ",
selection: [1, 3 + indent.length, 1, 3 + indent.length]
};
}
else {
return {
text: text + indent + " * ",
selection: [1, 3 + indent.length, 1, 3 + indent.length]
};
}

}
if (/\/\*\*/.test(line.substring(0, cursor.column))) {
return {
text: text + indent + " * " + text + " " + indent + "*/",
selection: [1, 4 + indent.length, 1, 4 + indent.length]
};
}
}
});
}
};


Expand Down
4 changes: 1 addition & 3 deletions src/mode/c_cpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var c_cppHighlightRules = require("./c_cpp_highlight_rules").c_cppHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var Range = require("../range").Range;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function() {
this.HighlightRules = c_cppHighlightRules;

this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = this.$defaultBehaviour;

this.foldingRules = new CStyleFoldMode();
};
Expand Down
3 changes: 1 addition & 2 deletions src/mode/crystal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ var TextMode = require("./text").Mode;
var CrystalHighlightRules = require("./crystal_highlight_rules").CrystalHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var Range = require("../range").Range;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var FoldMode = require("./folding/coffee").FoldMode;

var Mode = function() {
this.HighlightRules = CrystalHighlightRules;
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = this.$defaultBehaviour;
this.foldingRules = new FoldMode();
};
oop.inherits(Mode, TextMode);
Expand Down
3 changes: 1 addition & 2 deletions src/mode/csharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var CSharpHighlightRules = require("./csharp_highlight_rules").CSharpHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = require("./folding/csharp").FoldMode;

var Mode = function() {
this.HighlightRules = CSharpHighlightRules;
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = this.$defaultBehaviour;
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, TextMode);
Expand Down
1 change: 1 addition & 0 deletions src/mode/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Mode = function() {
CMode.call(this);
this.HighlightRules = DartHighlightRules;
this.foldingRules = new CStyleFoldMode();
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(Mode, CMode);

Expand Down
20 changes: 10 additions & 10 deletions src/mode/doc_comment_highlight_rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
var oop = require("../lib/oop");
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;

var DocCommentHighlightRules = function() {
var DocCommentHighlightRules = function () {
this.$rules = {
"start" : [ {
token : "comment.doc.tag",
regex : "@[\\w\\d_]+" // TODO: fix email addresses
},
DocCommentHighlightRules.getTagRule(),
{
defaultToken : "comment.doc",
caseInsensitive: true
}]
"start": [
{
token: "comment.doc.tag",
regex: "@\\w+(?=\\s|$)"
}, DocCommentHighlightRules.getTagRule(), {
defaultToken: "comment.doc",
caseInsensitive: true
}
]
};
};

Expand Down
1 change: 1 addition & 0 deletions src/mode/edifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var EdifactHighlightRules = require("./edifact_highlight_rules").EdifactHighligh
var Mode = function() {

this.HighlightRules = EdifactHighlightRules;
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(Mode, TextMode);

Expand Down
4 changes: 1 addition & 3 deletions src/mode/glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ var oop = require("../lib/oop");
var CMode = require("./c_cpp").Mode;
var glslHighlightRules = require("./glsl_highlight_rules").glslHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var Range = require("../range").Range;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function() {
this.HighlightRules = glslHighlightRules;

this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = this.$defaultBehaviour;
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, CMode);
Expand Down
3 changes: 1 addition & 2 deletions src/mode/golang.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var GolangHighlightRules = require("./golang_highlight_rules").GolangHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function() {
this.HighlightRules = GolangHighlightRules;
this.$outdent = new MatchingBraceOutdent();
this.foldingRules = new CStyleFoldMode();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(Mode, TextMode);

Expand Down
1 change: 1 addition & 0 deletions src/mode/groovy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var GroovyHighlightRules = require("./groovy_highlight_rules").GroovyHighlightRu
var Mode = function() {
JavaScriptMode.call(this);
this.HighlightRules = GroovyHighlightRules;
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(Mode, JavaScriptMode);

Expand Down
3 changes: 1 addition & 2 deletions src/mode/haxe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var HaxeHighlightRules = require("./haxe_highlight_rules").HaxeHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function() {
this.HighlightRules = HaxeHighlightRules;

this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = this.$defaultBehaviour;
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, TextMode);
Expand Down
78 changes: 40 additions & 38 deletions src/mode/ion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,52 @@
THIS FILE WAS GENERATED BY 'ligand' USING 'mode.js'
*/

"use strict";

var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var HighlightRules = require("./ion_highlight_rules").IonHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function() {
this.HighlightRules = HighlightRules;
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.foldingRules = new CStyleFoldMode();
"use strict";

var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var HighlightRules = require("./ion_highlight_rules").IonHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function () {
this.HighlightRules = HighlightRules;
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = this.$defaultBehaviour;
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, TextMode);

(function () {

this.lineCommentStart = "//";
this.blockComment = {
start: "/*",
end: "*/"
};
oop.inherits(Mode, TextMode);

(function() {
this.getNextLineIndent = function (state, line, tab) {
var indent = this.$getIndent(line);

this.lineCommentStart = "//";
this.blockComment = {start: "/*", end: "*/"};

this.getNextLineIndent = function(state, line, tab) {
var indent = this.$getIndent(line);

if (state == "start") {
var match = line.match(/^.*[\{\(\[]\s*$/);
if (match) {
indent += tab;
}
if (state == "start") {
var match = line.match(/^.*[\{\(\[]\s*$/);
if (match) {
indent += tab;
}
}

return indent;
};
return indent;
};

this.checkOutdent = function(state, line, input) {
return this.$outdent.checkOutdent(line, input);
};
this.checkOutdent = function (state, line, input) {
return this.$outdent.checkOutdent(line, input);
};

this.autoOutdent = function(state, doc, row) {
this.$outdent.autoOutdent(doc, row);
};
this.autoOutdent = function (state, doc, row) {
this.$outdent.autoOutdent(doc, row);
};

this.$id = "ace/mode/ion";
}).call(Mode.prototype);
this.$id = "ace/mode/ion";
}).call(Mode.prototype);

exports.Mode = Mode;
exports.Mode = Mode;
3 changes: 1 addition & 2 deletions src/mode/jack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var HighlightRules = require("./jack_highlight_rules").JackHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function() {
this.HighlightRules = HighlightRules;
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = this.$defaultBehaviour;
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, TextMode);
Expand Down
Loading

0 comments on commit 4fba678

Please sign in to comment.