Skip to content

Commit

Permalink
comment reflowing for c/c++
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Aug 21, 2012
1 parent 17a18a4 commit 040d9d3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
Expand Up @@ -43,7 +43,10 @@ public HashSet<AppCommand> getSupportedCommands(Commands commands)
{
HashSet<AppCommand> result = super.getSupportedCommands(commands);
if (isCpp())
{
result.add(commands.commentUncomment());
result.add(commands.reflowComment());
}
return result;
}

Expand Down
Expand Up @@ -56,6 +56,7 @@ public HashSet<AppCommand> getSupportedCommands(Commands commands)
result.add(commands.jumpTo());
result.add(commands.goToFunctionDefinition());
result.add(commands.insertSection());
result.add(commands.codeCompletion());
return result;
}
}
Expand Up @@ -120,9 +120,9 @@ well as menu structures (for main menu and popup menus).
<cmd refid="goToFunctionDefinition"/>
<separator/>
<cmd refid="extractFunction"/>
<cmd refid="commentUncomment"/>
<cmd refid="reindent"/>
<cmd refid="reflowComment"/>
<cmd refid="commentUncomment"/>
<separator/>
<cmd refid="executeCode"/>
<cmd refid="executeLastCode"/>
Expand Down
Expand Up @@ -228,6 +228,7 @@ public Source(Commands commands,
dynamicCommands_.add(commands.debugImportDump());
dynamicCommands_.add(commands.goToLine());
dynamicCommands_.add(commands.checkSpelling());
dynamicCommands_.add(commands.codeCompletion());
for (AppCommand command : dynamicCommands_)
{
command.setVisible(false);
Expand Down
Expand Up @@ -1365,6 +1365,14 @@ void onReindent()

@Handler
void onReflowComment()
{
if (fileType_.isCpp())
doReflowComment("(//)");
else
doReflowComment("(#)");
}

void doReflowComment(String commentPrefix)
{
docDisplay_.focus();

Expand All @@ -1373,7 +1381,7 @@ void onReflowComment()

if (selection.isEmpty())
{
selection = selection.growToIncludeLines("^\\s*#.*$");
selection = selection.growToIncludeLines("^\\s*" + commentPrefix + ".*$");
}
else
{
Expand All @@ -1384,23 +1392,26 @@ void onReflowComment()
if (selection.isEmpty())
return;

reflowComments(selection, originalSelection.isEmpty() ?
originalSelection.getStart() :
null);
reflowComments(commentPrefix,
selection,
originalSelection.isEmpty() ?
originalSelection.getStart() :
null);
}

private Position selectionToPosition(InputEditorPosition pos)
{
return docDisplay_.selectionToPosition(pos);
}

private void reflowComments(InputEditorSelection selection,
private void reflowComments(String commentPrefix,
InputEditorSelection selection,
final InputEditorPosition cursorPos)
{
String code = docDisplay_.getCode(selection);
String[] lines = code.split("\n");
String prefix = StringUtil.getCommonPrefix(lines, true);
Pattern pattern = Pattern.create("^\\s*#+('?)\\s*");
Pattern pattern = Pattern.create("^\\s*" + commentPrefix + "+('?)\\s*");
Match match = pattern.match(prefix, 0);
// Selection includes non-comments? Abort.
if (match == null)
Expand Down
Expand Up @@ -217,9 +217,9 @@ private Widget createCodeTransformMenuButton()
menu.addItem(commands_.goToFunctionDefinition().createMenuItem(false));
menu.addSeparator();
menu.addItem(commands_.extractFunction().createMenuItem(false));
menu.addItem(commands_.commentUncomment().createMenuItem(false));
menu.addItem(commands_.reindent().createMenuItem(false));
menu.addItem(commands_.reflowComment().createMenuItem(false));
menu.addItem(commands_.commentUncomment().createMenuItem(false));
codeTransform_ = new ToolbarButton("", icon, menu);
codeTransform_.setTitle("Code Tools");
}
Expand All @@ -239,7 +239,10 @@ public void adaptToFileType(TextFileType fileType)
srcOnSaveLabel_.setText("Preview on Save");
else
srcOnSaveLabel_.setText("Source on Save");
codeTransform_.setVisible(canExecuteCode && !fileType.canAuthorContent());
codeTransform_.setVisible(
(canExecuteCode && !fileType.canAuthorContent()) ||
fileType.isCpp());

sourceButton_.setVisible(canExecuteCode && !canExecuteChunks);
sourceMenuButton_.setVisible(canExecuteCode && !canExecuteChunks);

Expand Down

0 comments on commit 040d9d3

Please sign in to comment.