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"

 后将代码修改为两个快捷键,一个 加注释 一个去注释

转载于:https://www.cnblogs.com/sitemaker/archive/2012/08/07/2626967.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值