jEdit Community - Resources for users of the jEdit Text Editor
Block comment dynamic expand
Submitted by alexandrei on Tuesday, 14 April, 2009 - 08:27
Hello,

I am curious if there is a plugin/macro that can do the following thing:
- I manually open a block comment (c/c++, etc), with "/*" or "/**"
- I press enter, and a new * is added at the start of the line, aligned with the previous * (1 line indent)
- the above repeats, until I enter the ending "/"

I've seen this behaviour in vi/vim, and I really like it! This is the only thing that I could not find in jEdit (yet). Otherwise, it's my perfect editor Laughing out loud

Thank you!
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
I use a macro for such commen
by daleanson on Wed, 15/04/2009 - 17:39
I use a macro for such comments. It's not exactly as you're asking for, but gets the same end result. What I do is type in the comment starting at the left edge of the text area, select it, then run the macro. It inserts the stars and indents appropriately. Here is the macro:

/*
* DocComment.bsh - a BeanShell macro script for the
* jEdit text editor - puts the selected text in a JavaDoc-style
* comment block
* Copyright (C) 2002 Dale Anson
* danson@germane-software.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the jEdit program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/


void docComment() {

// check buffer read-only status
if ( buffer.isReadOnly() ) {
Macros.error( view, "File is read only." );
return ;
}

// get the current selection or the current line if no selection
Selection[] selections = textArea.getSelection();

// this doesn't work right with multiple selection, so don't do anything
if ( selections.length > 1 ) {
Macros.error( view, "Doc comment does not work with multiple selection." );
return ;
}

String ls = buffer.getStringProperty( "lineSeparator" );


// make sure last line ends with a line separator
String line_text = textArea.getLineText( textArea.getLineCount() - 1 );
if ( !line_text.endsWith( ls ) && textArea.getCaretLine() >= textArea.getLineCount() - 3 ) {
buffer.insert( buffer.getLength(), ls );
}

// if nothing is selected, select current line
if ( selection == null || selections.length == 0 ) {
int line = textArea.getCaretLine();
selections = new Selection[ 1 ];
int start_offset = textArea.getLineStartOffset( line );
int end_offset = textArea.getLineEndOffset( line );
selections[ 0 ] = new Selection.Range( start_offset, end_offset );
textArea.addToSelection( selections[ 0 ] );
}

// adjust the start and end line offsets to reselect after the move
int startLine = selections[ 0 ].getStartLine();
int endLine = selections[ 0 ].getEndLine();

// insert stars
for (int i = startLine; i < endLine; i++) {
buffer.insert(textArea.getLineStartOffset(i), "* ");
}

// insert start and end of comment
buffer.insert( textArea.getLineStartOffset( startLine ), "/**" + ls );
buffer.insert( textArea.getLineStartOffset( endLine + 1 ), "*/" + ls );

// indent lines
buffer.indentLines( startLine - 1, endLine + 1 );

// insert spaces so the stars line up nice
for (int i = startLine + 1; i < endLine + 2; i++) {
buffer.insert(textArea.getLineStartOffset(i), " ");
}

// reselect the changed text
textArea.selectNone();
selection = new Selection.Range(
textArea.getLineStartOffset( startLine ),
textArea.getLineStartOffset( endLine + 2 )
);
textArea.addToSelection( selection );
}

docComment();
 
Hello, Thank you for your
by alexandrei on Tue, 05/05/2009 - 11:50
Hello,

Thank you for your macro. I just gave it a try, and it's ok, but indeed, not exactly what I was looking for.

I do something similar, by writing the comment directly, then using block insert, for adding "* ".

In both cases, there's a little strange when you have to update the comment, and add lines.

Eh.. i'll find a solution; if not.. it's nothing critical.

Have a nice day, and thank you again Smiling
User login
Browse archives
« March 2024  
MoTuWeThFrSaSu
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   82339
Context Free Art (*.cfdg)   0.31   46046
JBuilder scheme   .001   18487
BBEdit scheme   1.0   18108
ColdFusion scheme   1.0   18016
R Edit Mode - extensive version   0.1   17465
Advanced HTML edit mode   1.0   16198
Matlab Edit Mode   1.0   16060
jEdit XP icons   1.0   15221
XP icons for jEdit   1.1   14285