们可以使用语法高亮功能从SublimeText中的源文件中删除所

们可以使用语法高亮功能从SublimeText中的源文件中删除所

本文介绍了我们可以使用语法高亮功能从SublimeText中的源文件中删除所有注释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一堆用不同语言编写的源文件,我想删除源文件中的所有注释。 编写regex当然是一个选项,根据输入文件,我可能不得不处理的字符表示注释出现在字符串文字内的情况。还需要维护不同语言的正则表达式列表。 语法高亮似乎在突出显示注释方面做得很好,但是没有似乎是任何命令删除命令选项板中的所有注释。 有任何方法利用SublimeText中的语法高亮功能从不同的源文件中删除所有注释语言?解决方案根据nhahtdh的回答,以下插件应该适用于Sublime Text 2和3 import sublime_plugin class RemoveCommentsCommand(sublime_plugin.TextCommand): def run(self,edit): = self.view.find_by_selector('comment')区域的反转(注释): self.view.erase(edit,region) pre> 使用Python语法创建一个新文件,并将上面的代码粘贴到其中。将文件保存在您的 Packages / User 目录中(可通过 首选项 - >浏览软件包... )为 remove_comments.py 。您现在可以通过控制台运行插件,或绑定一个组合键。要通过控制台运行,只需键入 view.run_command('remove_comments') ,并且当前视图中的所有注释都将被删除。 要绑定组合键,请打开 首选项 - >键盘绑定 - 用户 并添加以下内容(如果文件为空,请用方括号 [] > {keys:[ctrl + alt + shift + r],command: remove_comments} 保存文件,现在可以按 Shift (或您选择的任何键组合),并且当前文件中的所有注释都将被删除。 I have a bunch of source files written in different languages, and I would like to strip all comments from the source files.While writing regex is certainly an option, depending on the input files, I may have to handle cases where the character to denote comment appears inside string literals. There is also the need to maintain a list of regex for different languages.The syntax highlighting seems to do quite a good job at highlighting the comments, but there doesn't seem to be any command to remove all comments in the Command Palette.Is there any way to leverage the syntax highlighting feature in SublimeText to remove all comments from source files in different languages? 解决方案 Based on nhahtdh's answer, the following plugin should work for both Sublime Text 2 and 3import sublime_pluginclass RemoveCommentsCommand(sublime_plugin.TextCommand): def run(self, edit): comments = self.view.find_by_selector('comment') for region in reversed(comments): self.view.erase(edit, region)Create a new file with Python syntax, and paste the code above into it. Save the file in your Packages/User directory (accessible via Preferences -> Browse Packages...) as remove_comments.py. You can now either run the plugin via the console, or bind a key combination to it. To run via the console, just type view.run_command('remove_comments')in the console, and all the comments in the current view will be deleted.To bind a key combination, open Preferences -> Key Bindings-User and add the following (surround it with square brackets [] if the file is empty):{ "keys": ["ctrl+alt+shift+r"], "command": "remove_comments" }Save the file, and you can now hit (or whatever key combination you choose) and all comments in the current file will be deleted. 这篇关于我们可以使用语法高亮功能从SublimeText中的源文件中删除所有注释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 10:47