2

我正在尝试创建一个片段以在 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*

4

1 回答 1

2

您可以使用regexp在片段中拆分字符串。但我建议创建插件而不是片段。您可以使用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
于 2016-08-20T14:08:46.223 回答