Skip to content

refactor ParsedIntent to use pimpl pattern#318

Merged
Johnson-zs merged 2 commits into
linuxdeepin:semantic-searchfrom
Johnson-zs:semantic-search
Jun 24, 2026
Merged

refactor ParsedIntent to use pimpl pattern#318
Johnson-zs merged 2 commits into
linuxdeepin:semantic-searchfrom
Johnson-zs:semantic-search

Conversation

@Johnson-zs

Copy link
Copy Markdown
Contributor
  • refactor: convert HighlightOptions to Pimpl pattern
  • fix: refactor ParsedIntent to use pimpl pattern

Changed the HighlightOptions from a plain struct to a Pimpl (Pointer
to Implementation) class pattern with QSharedDataPointer for implicit
sharing (COW - Copy On Write) support. This provides better ABI
stability and encapsulation.

Key changes:
1. Converted plain struct to class with private implementation
2. Added getter/setter methods for all properties
3. Implemented proper copy/move semantics
4. Added QSharedDataPointer for efficient copying
5. Updated all usage sites to use new accessor methods

The change was made to:
1. Improve ABI stability - changes to implementation won't break binary
compatibility
2. Enable future extensibility without breaking changes
3. Reduce header dependencies through proper encapsulation
4. Allow implicit sharing for efficient copying
5. Better control over member access

The Pimpl pattern gives better binary compatibility guarantees and
prepares the codebase for future extensions while maintaining the same
functionality.

Log: Updated HighlightOptions implementation to use Pimpl pattern

Influence:
1. Verify snippet highlight extraction still works in file search
results
2. Test content preview generation in search results
3. Check search functionality in file manager
4. Validate highlighter works with different preview length settings
5. Verify HTML wrapping option works when enabled

refactor: 将 HighlightOptions 转换为 Pimpl 模式

将 HighlightOptions 从普通结构体转换为使用 Pimpl(Pointer to
Implementation)模式和 QSharedDataPointer 支持隐式共享(COW - 写入时复
制)的类。这提供了更好的ABI稳定性和封装性。

主要变更:
1. 将普通结构体转换为具有私有实现的类
2. 为所有属性添加getter/setter方法
3. 实现适当的拷贝/移动语义
4. 添加QSharedDataPointer以支持高效复制
5. 更新所有使用点以使用新的访问方法

此次更改的目的:
1. 提高ABI稳定性 - 实现更改不会破坏二进制兼容性
2. 支持未来扩展性而无需破坏性更改
3. 通过适当封装减少头文件依赖
4. 允许隐式共享以提高复制效率
5. 更好地控制成员访问

Pimpl模式提供了更好的二进制兼容性保证,并使代码库准备好未来扩展,同时保
持相同功能。

Log: 更新 HighlightOptions 实现以使用 Pimpl 模式

Influence:
1. 验证文件搜索结果中的片段高亮提取功能仍正常工作
2. 测试搜索结果中内容预览生成
3. 检查文件管理器中的搜索功能
4. 验证高亮器在不同的预览长度设置下工作正常
5. 验证HTML包装选项在启用时正常工作
1. Change ParsedIntent from struct to class with QSharedDataPointer
2. Add private implementation class ParsedIntentPrivate
3. Implement proper copy/move semantics
4. Add getter/setter methods for all fields
5. Update all usage points to use new accessors

Influence:
1. All test cases involving ParsedIntent need to be verified
2. Semantic search functionality should work as before
3. Pay attention to deep copy behavior and performance impact

fix: 重构 ParsedIntent 使用 pimpl 模式

1. 将 ParsedIntent 从结构体改为使用 QSharedDataPointer 的类
2. 添加私有实现类 ParsedIntentPrivate
3. 实现正确的拷贝/移动语义
4. 为所有字段添加 getter/setter 方法
5. 更新所有使用点以使用新的访问器

影响:
1. 需要验证所有涉及 ParsedIntent 的测试用例
2. 语义搜索功能应保持原有行为
3. 注意深拷贝行为和性能影响

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @Johnson-zs, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码完美实现了ParsedIntent和HighlightOptions从裸struct到Pimpl模式的架构重构,所有调用点均准确适配
逻辑正确且无任何安全风险,完全符合Qt隐式共享与ABI稳定性设计规范

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

重构将直接暴露成员变量的struct改为基于QSharedDataPointer的class,并提供了const与非const重载的getter及setter。拷贝/移动构造函数及赋值运算符均正确使用=default,依赖QSharedDataPointer自动实现COW语义与内存管理。所有Extractor、Builder、JsonOutput及测试代码中的属性访问均从点操作符(如intent.timeConstraint.kind)准确替换为getter调用(如intent.timeConstraint().kind),赋值操作准确替换为setter调用(如intent.setKeywords()),无任何遗漏或编译不匹配问题。

  • 2.代码质量(良好)✓

头文件中清晰注释了Pimpl模式的设计意图与ABI稳定性保障,类声明遵循Qt命名规范。Pimpl实现文件parsedintent.cpp与highlightoptions_p.h职责划分清晰。在actionextractor.cpp与locationextractor.cpp中存在对同一getter的连续两次调用(如intent.searchDirectories().contains(path)与intent.searchDirectories().append(path)),虽然因QSharedDataPointer的detach机制保证逻辑正确,但略显冗余。
建议:在actionextractor.cpp和locationextractor.cpp中,可通过局部引用变量缓存getter返回值以提升代码可读性,例如QStringList &dirs = intent.searchDirectories();

  • 3.代码性能(无性能问题)✓

引入QSharedDataPointer增加了一次指针解引用开销,但对于文件搜索这种IO密集型场景可完全忽略。在单线程解析流程中传递ParsedIntent引用不会触发不必要的深拷贝。测试用例中多次调用timeConstraint()等getter返回的是引用,不涉及大量数据拷贝。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次提交为纯架构重构,仅改变了数据成员的访问方式(从直接暴露改为getter/setter),未引入任何新的外部输入解析、命令执行、路径拼接或权限校验逻辑,彻底杜绝了引入新安全漏洞的可能。

■ 【改进建议代码示例】

// src/dfm-search/dfm-search-lib/semantic/extractors/actionextractor.cpp
// 优化连续调用getter的代码片段,提升可读性并避免冗余的detach检查
void ActionExtractor::extract(const QString &input, ParsedIntent &intent)
{
    // ... 前置逻辑省略 ...

        // ── IM received files ──
        if (isImReceive) {
            const QStringList resolvedPaths = resolveImReceivedPaths();
            if (!resolvedPaths.isEmpty()) {
                // 使用局部引用缓存getter返回值,避免重复调用
                QStringList &dirs = intent.searchDirectories();
                for (const QString &path : resolvedPaths) {
                    if (!dirs.contains(path)) {
                        dirs.append(path);
                    }
                }

                MatchSpan span;
                span.start = m.capturedStart();
                span.end = m.capturedEnd();
                span.ruleId = ruleIds[i];
                intent.consumedSpans().append(span);
            }
            continue;
        }

    // ... 后置逻辑省略 ...
}

@Johnson-zs Johnson-zs merged commit 140312b into linuxdeepin:semantic-search Jun 24, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants