Scintilla添加批量注释功能
下载scite编译 发现支持批量块注释功能,查看scite源码,SciTEBase.cxx文件的StartBlockComment() 函数
引用了SString 类,而SString 的成员函数在 PropSetFile.cxx 函数中定义
将PropSetFile.cxx 重命名为SString.cpp,去掉与SString 无关的定义,包含文件保留
#include "StdAfx.h"
#include "SString.h"
#include <sstream>
将 wEditor.Call 修改为SendMessage
去掉StartBlockComment()函数 前面的读取配置文件的定义
代码如下
bool CScintillaWnd::EndBlockComment() {
bool placeCommentsAtLineStart = false;
SString comment = "#"; //#号注释
SString long_comment = comment;
long_comment.append(" ");
int selectionStart = SendMessage(SCI_GETSELECTIONSTART);
int selectionEnd = SendMessage(SCI_GETSELECTIONEND);
int caretPosition = SendMessage(SCI_GETCURRENTPOS);
// checking if caret is located in _beginning_ of selected block
bool move_caret = caretPosition < selectionEnd;
int selStartLine = SendMessage(SCI_LINEFROMPOSITION, selectionStart);
int selEndLine = SendMessage(SCI_LINEFROMPOSITION, selectionEnd);
int lines = selEndLine - selStartLine;
int firstSelLineStart = SendMessage(SCI_POSITIONFROMLINE, selStartLine);
// "caret return" is part of the last selected line
if ((lines > 0) &&
(selectionEnd == SendMessage(SCI_POSITIONFROMLINE, selEndLine)))
selEndLine--;
SendMessage(SCI_BEGINUNDOACTION);
for (int i = selStartLine; i <= selEndLine; i++) {
int lineStart = SendMessage(SCI_POSITIONFROMLINE, i);
int lineIndent = lineStart;
int lineEnd = SendMessage(SCI_GETLINEENDPOSITION, i);
if (!placeCommentsAtLineStart) {
lineIndent = GetLineIndentPosition(i);
}
SString linebuf = GetRange(lineIndent, lineEnd);
// empty lines are not commented
if (linebuf.length() < 1)
continue;
if (linebuf.startswith(comment.c_str())) {
int commentLength = static_cast<int>(comment.length());
if (linebuf.startswith(long_comment.c_str())) {
// Removing comment with space after it.
commentLength = static_cast<int>(long_comment.length());
}
SendMessage(SCI_SETSEL, lineIndent, lineIndent + commentLength);
SendMessage(SCI_REPLACESEL, 0, reinterpret_cast<LPARAM>(""));
if (i == selStartLine) // is this the first selected line?
selectionStart -= commentLength;
selectionEnd -= commentLength; // every iteration
continue;
}
}
// after uncommenting selection may promote itself to the lines
// before the first initially selected line;
// another problem - if only comment symbol was selected;
if (selectionStart < firstSelLineStart) {
if (selectionStart >= selectionEnd - (static_cast<int>(long_comment.length()) - 1))
selectionEnd = firstSelLineStart;
selectionStart = firstSelLineStart;
}
if (move_caret) {
// moving caret to the beginning of selected block
SendMessage(SCI_GOTOPOS, selectionEnd);
SendMessage(SCI_SETCURRENTPOS, selectionStart);
} else {
SendMessage(SCI_SETSEL, selectionStart, selectionEnd);
}
SendMessage(SCI_ENDUNDOACTION);
return true;
}
在头文件中包含 #include "SString.h"
后将代码修改为两个快捷键,一个 加注释 一个去注释