sublimetext2 - 替换另一个文件中的匹配选择

标签 sublimetext2 sublimetext3

我正在做多语言翻译工作,

如您所见,如果右侧文档匹配,则应将其替换为左侧文档。

inline

我怎样才能用 Sublime text 做到这一点?

因为它们不是 1-1 映射

我的预期结果是

inline

最佳答案

我编写了一个概念证明,允许您替换(左)和目标(右)之间的匹配选择) 文件。

 

演示:

Demo

 



 

用法:

 

使用 MatchReplace:

  • 将插件文件夹复制到您的Packages目录
  • 编辑 run 函数中的 replacementKeys 数组以满足您的匹配+替换需求
  • 保存插件并重新启动 SublimeText
  • 使用 Shift + Alt + 2 打开 2 组窗口布局
  • 将您想要作为 SOURCE 的文档移至左侧组
  • 将要与源匹配的文档移至右侧组
  • 使用 Ctrl + Shift + P 打开命令面板并运行 Match Replace: Demo 命令

 

注意:

replacementKeys 必须在两个文档中完全匹配(忽略前导空格)。

如果您希望在 replacementKeys 中允许变化,则需要实现额外的 RegEx 处理层。

 



 

实现:

 

  • 获取 2 组窗口中每个组的事件 View
  • 查找将在两个文档中匹配的用户定义键数组的 RegEx 匹配项
  • 存储两个文档的值区域以及源文档的字符串值
  • 按区域对存储的值进行排序
  • 迭代目标文档中的区域,替换源中的所有匹配值

 

该演示是为处理单层 JSON 文件而编写的,但可以根据需要进行调整。

replacementKeys 之前和之后的

RegEx 模式为:

  • queryPrefix
  • querySuffix

 



 

代码:

 

使用我的自定义 Edit 模块,该脚本的工作会更加顺利,因此我建议您在此处下载整个插件:

@ GitHub

 

import sublime, sublime_plugin
import Edit
import operator

class MatchReplaceDemoCommand ( sublime_plugin.TextCommand ):

    def run ( self, edit ):

        replacementKeys = []

        #■■■  Populate With Keys To Be Replaced In Document2  ■■■#
        replacementKeys.append ( "flight_number" )
        replacementKeys.append ( "price" )
        replacementKeys.append ( "payment" )

        self.replace_KeyValues ( replacementKeys )

    def replace_KeyValues ( self, replacementKeys ):

        window = self.view.window()

        document1_ResultStrings = {}
        document2_ResultRegions = {}

        #■■■  Verify : 2 Active Window Groups  ■■■#
        windowGroup_Count = window.num_groups()

        if windowGroup_Count != 2:
            return

        #■■■  Set : Document Views  ■■■#
        document1 = window.active_view_in_group ( 0 ) # Document 1 == SOURCE
        document2 = window.active_view_in_group ( 1 ) # Document 2 == MATCH
        edit = Edit.get ( document2 )

        #■■■  Set : Seach Parameters  ■■■#
        query_StartPosition = 0
        queryPrefix         = "((^)|(^[\t\ ]+))"
        querySuffix         = ":"

        #■■■  Store : KeyValue Regions & Strings  ■■■#
        for key in replacementKeys:

            #■■■  Find Document1 Key Regions & Strings  ■■■#
            document1_KeyRegion          = document1.find ( queryPrefix + key + querySuffix, query_StartPosition )
            document1_ResultRegion_Start = document1_KeyRegion.b
            document1_ResultRegion_End   = document1.line ( document1_KeyRegion ).b
            document1_ResultRegion       = sublime.Region ( document1_ResultRegion_Start, document1_ResultRegion_End )
            document1_ResultString       = document1.substr ( document1_ResultRegion )

            #■■■  Find Document2 Key Regions  ■■■#
            document2_KeyRegion          = document2.find ( queryPrefix + key + querySuffix, query_StartPosition )
            document2_ResultRegion_Start = document2_KeyRegion.b
            document2_ResultRegion_End   = document2.line ( document2_KeyRegion ).b
            document2_ResultRegion       = sublime.Region ( document2_ResultRegion_Start, document2_ResultRegion_End )

            #■■■  Verify Match  ■■■#
            if  document1_ResultRegion_Start != -1 \
            and document2_ResultRegion_Start != -1:
                document1_ResultStrings[ key ] = document1_ResultString
                document2_ResultRegions[ key ] = document2_ResultRegion

        #■■■  Verify : Matches Found  ■■■#
        if len ( document1_ResultStrings ) == 0 \
        or len ( document2_ResultRegions ) == 0:
            return

        #■■■  Sort Regions To Avoid Replacement Overlap  ■■■#
        document2_ResultRegions = sorted ( document2_ResultRegions.items(), key=operator.itemgetter ( 1 ) )

        #■■■  Replace Matched KeyValues  ■■■#
        for key, value  in reversed ( document2_ResultRegions ):
            replacementField  = key
            replacementRegion = value
            replacementString = document1_ResultStrings[ key ]
            edit.replace ( replacementRegion, replacementString )

关于sublimetext2 - 替换另一个文件中的匹配选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36242643/

相关文章:

indentation - sublime text 2 中的可见制表符宽度

html - 我的 CSS 文件没有保存

vim - 在 Sublime Text 复古模式下取消选择文本,无需转义键

sublimetext2 - 折叠/折叠 sublime text 2 中的异常(exception)代码部分

macos - Mac OS X 上多个光标的 Sublime Text 热键/键绑定(bind)

sublimetext3 - 如何在 SublimeText 中将每一行用引号引起来?

sublimetext2 - Sublime Text 打开新窗口 API

sublimetext - 列出 Sublime Text 3 中所有已安装软件包的最实用方法是什么?

sublimetext3 - 在 Sublime 3 中忽略文件搜索中的 node_modules

java - 如何编译和运行包含 Import with Sublime Text 2 的 Java 程序?