随笔- 2  文章- 0  评论- 0  阅读- 8 

前提条件:

  • 官网下载Qscintilla库,并使用QtCreator构建出静态库。

  • 将Qsci文件夹和生成的库拷到准备使用的项目文件下


正文

  1. 实现自己的customqscintilla

     // 自动缩进
     editor->setTabWidth(4);
     editor->setIndentationWidth(4);
     editor->setIndentationsUseTabs(false);
     editor->setAutoIndent(true);
    
     // 当前行高亮
     editor->setCaretLineVisible(true);
     editor->setCaretLineBackgroundColor(QColor("#E8E8FF"));
     editor->setCaretWidth(3);
    
     // 词法分析器
     auto textLexer = new QsciLexerMat(editor);
     editor->setLexer(textLexer);
    
     // 行号
     editor->setMarginLineNumbers(1, true);
     editor->setMarginWidth(1, "000");
    
     // 代码折叠
     editor->setFolding(QsciScintilla::BoxedTreeFoldStyle);
     editor->setMarginSensitivity(2, true); // 确保可以点击边距进行折叠
     editor->setMarginType(2, QsciScintilla::SymbolMargin);
    
     auto api = new QsciAPIs(textLexer);
     // 读取关键字文件
     if (!api->load("C:/Users/12117/Desktop/QtWidgetsApplication1/QtWidgetsApplication1/QtWidgetsApplication1/apis.txt"))
     {
         QMessageBox::warning(this, "Warning", "Load Failed");
     }
     else
     {
         api->prepare();
     }
    
     // 自动补全
     editor->setAutoCompletionSource(QsciScintilla::AcsAll);
     editor->setAutoCompletionCaseSensitivity(true);
     editor->setAutoCompletionReplaceWord(true);
     editor->setAutoCompletionUseSingle(QsciScintilla::AcusNever);
     editor->setAutoCompletionThreshold(1)
    

  1. 继承QsciLexerCustom实现自己的自定义词法分析器

     class QsciLexerMat : public QsciLexerCustom
     {
     Q_OBJECT
    
     public:
       QsciLexerMat(QObject* parent);
       ~QsciLexerMat();
    
    
     virtual void styleText(int start, int end);
     virtual const char* language() const;
     virtual QString description(int style) const;
     QColor defaultColor(int style) const;
     virtual const char* keywords(int set) const;
    
    
     enum {
     	SCE_C_DEFAULT = 0,
     	SCE_C_COMMENTLINE,
     	SCE_C_STRING,
     	SCE_C_NUMBER,
     	SCE_C_KEYWORD,
     	SCE_C_IDENTIFIER
     };
    };  
    

  1. 重点实现styleText函数(以下代码仅作示例)

     ///----省略----////
     auto handleString = [&]() {
         QChar delimiter = text[pos];
         styleBuffer[pos] = SCE_C_STRING;
         ++pos;
         while (pos < length) {
             if (text[pos] == '\\') { // 转义字符
                 if (pos + 1 < length) {
                     styleBuffer[pos] = SCE_C_STRING;
                     styleBuffer[pos + 1] = SCE_C_STRING;
                     pos += 2;
                 }
                 else {
                     break;
                 }
             }
             else if (pos < length && text[pos] == delimiter) {
                 styleBuffer[pos] = SCE_C_STRING;
                 ++pos;
                 inString = false;
                 break;
             }
             else if (pos < length) {
                 styleBuffer[pos] = SCE_C_STRING;
                 ++pos;
             }
             else {
                 break;
             }
         }
         };  
     ///----省略----////
     while (pos < length) {
     QChar ch = text.at(pos);
    
     // 处理字符串,注释,关键字和标识符,数字
     if (!inString && (ch == '\'' || ch == '"')) {
         inString = true;
         handleString();
         continue;
     }  
    
     ////----省略----////  
    
     // 应用样式  
       startStyling(start);
       int currentBytePos = start;
       int currentStyle = -1;
       int count = 0;
       QTextCodec* codec = QTextCodec::codecForName("UTF-8");
       if (!codec) {
           qWarning("Failed to get UTF-8 codec");
           return;
       }
    
       QByteArray byteArray;
       for (int i = 0; i < styleBuffer.size(); ++i) {
           if (styleBuffer[i] != currentStyle) {
               if (count > 0) {
                   byteArray = codec->fromUnicode(text.mid(i - count, count));
                   setStyling(byteArray.length(), currentStyle); // 设置相同样式的字符
                   currentBytePos += byteArray.length();
               }
               currentStyle = styleBuffer[i];
               count = 0;
           }
           count++;
       }
       if (count > 0) {
           byteArray = codec->fromUnicode(text.mid(text.length() - count, count));
           setStyling(byteArray.length(), currentStyle); // 设置最后一段相同样式的字符
           currentBytePos += byteArray.length();
       }
    
 posted on   哈哈哈119  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示