【问题标题】:Sublime Text 3 Get and Set Snippet JavaSublime Text 3 获取和设置 Snippet Java
【发布时间】:2016-08-10 00:46:52
【问题描述】:

我正在尝试创建一个 sn-ps 以在 java 中自动生成 getter 和 setter

问题是我不知道如何拆分取自 $TM_SELECTED_TEXT

的字符串

代码需要在构造函数下面插入

选择文本后,它看起来像这样 字符串名称

这是代码,我错过了拆分字符串和在构造函数下插入代码,因为我不知道该怎么做

<snippet>
  <content><![CDATA[
    public void set$TM_SELECTED_TEXT($TM_SELECTED_TEXT $TM_SELECTED_TEXT) {
      this.$TM_SELECTED_TEXT = $TM_SELECTED_TEXT;
    }

    public $TM_SELECTED_TEXT get$TM_SELECTED_TEXT {
      return this.$TM_SELECTED_TEXT;
    }
  ]]></content>
  <tabTrigger>getter_setter</tabTrigger>
  <scope>source.java</scope>
</snippet>

我也想知道如何将 varname 的第一个字母更改为大写,以使其看起来 getNamesetName 而不是 setnamegetname*

【问题讨论】:

    标签: java sublimetext-snippet


    【解决方案1】:

    您可以使用 regexp 在 sn-p 中拆分字符串。但我建议创建插件 instad 的 sn-p。您可以使用 sublime 插件中的 python split() 函数和 capitalize() 将 varname 的第一个字母更改为大写。

    要访问插件中的选择,请使用以下结构:

    self.view.sel()[0]
    

    插入 getter 或 setter 代码使用:

    self.view.replace(edit, region, content)
    

    或:

    self.view.insert(edit, position, content)
    

    此代码适用于我:

    import sublime, sublime_plugin
    
    class javamagicCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            snippet_code = ''' public void set$TM_SELECTED_PART1($TM_SELECTED_PART1 $TM_SELECTED_PART2) {
          this.$TM_SELECTED_PART1 = $TM_SELECTED_PART1;
        }'''
            new_content = ''
    
            for selection in self.view.sel():
                selection_content = self.view.substr(selection)
                if selection_content.find('.') > 0:
                    parts = selection_content.split('.')
                    new_content = snippet_code.replace('$TM_SELECTED_PART1', parts[0])
                    new_content = new_content.replace('$TM_SELECTED_PART2', parts[1])
                    self.view.insert(edit, selection.begin(), new_content)
                else:
                    sublime.status_message('wrong selection') # statusline message
                    # sublime.message_dialog('wrong selection') # popup message
    
            for selection in self.view.sel():
                self.view.erase(edit, selection)
    
            #print('done') # debug output to console
    

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      相关资源
      最近更新 更多