http://www.sublimetext.com/docs/plugin-basics

http://code.tutsplus.com/tutorials/how-to-create-a-sublime-text-2-plugin--net-22685

http://code.tutsplus.com/tutorials/sublime-text-2-tips-and-tricks--net-21519

 

 

examples : 

http://www.sublimetext.com/docs/plugin-examples

 

 

api references : 

http://www.sublimetext.com/docs/api-reference

 


 

 

대략의 설명

 

패키지는 폴더로 만들어 져서 Package 디렉토리에 넣으면 된다. 

나의 패키지는 "Preferences > Browse Packages…" 메뉴를 통해 들어가면 된다.

패키지는 zip 파일로 묶은 다음 확장자를 .sublime-package 로 바꿔 단일 파일로 사용 가능하다. 

 

보통 language 관련 패키지가 있고, 그 외 Default, User 패키지가 있다.

  • Default : 모든 표준 키 바인딩, 메뉴 정의, 파일 설정, 파이썬으로 만든 모든 플러그인
  • User : 마지막에 로딩 되어 기본 값들을 덮어 씀

 

 

기본 구현

Tools > New Plugin... 메뉴 선택

1
2
3
4
5
import sublime, sublime_plugin
 
class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0"Hello, World!")

 

바로 이런 게 생긴다.

 

일단  Packages/ 에서 폴더를 하나 만들고 그 안에 py 파일로 저장한다.

파일 이름은 뭐든 관계 없고, 관례적으로 플러그인 이름을 사용한다.

 

저장이 되었으면, 콘솔을 열고 (단축키 : Ctrl + ' ) 다음과 같이 입력하면

view.run_command('example')

 

파일 첫 부분에 "Hello World" 가 삽입된다.

 

Command Type

Sublime plugin 은 다음 세 가지 command type 을 제공한다.

  • Text Commands : View 오브젝트를 통해 file 이나 buffer 에 접근
  • Window Commands : Window 오브젝트를 통해 현재 창을 참조
  • Applications Commands : 위 두가지에 속 하지 않는.

 

import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0"Hello, World!")
sublime_plugin.TextCommand 를 상속 받은 class 의 이름에서 'Command' 를 제외한 나머지 부분,
CamelCase 형식의 이름을 under_score 형식으로 변경한 것이 커맨드 이름이 된다.

(예를 들면, 클래스 이름이 MyOwnCommand 라면 my_own 이 커맨드 이름이 된다.)

 

 

 

단축키 추가하기

패키지 안에 다음과 같이 파일을 추가한다.

MyPackage/
    Default (Linux).sublime-keymap
    Default (OSX).sublime-keymap
    Default (Windows).sublime-keymap

 

Preferences > Key Bindings – Default 를 통해 겹치지 않는 단축키를 택하면 된다.

다음과 같이 추가하면 된다.

[
    {
        "keys": ["ctrl+alt+x"], "command""command_name" 
    }
]

커맨드의 인자가 필요하면 뒤에다 arg 로 붙인다.

OSX 에서 커맨드 키는 super 라고 쓴다.

 

 

메뉴 추가하기

대략 .sublime-menu 파일을 만들면 된다.

  • Main.sublime-menu : 메인 메뉴
  • Side Bar.sublime-menu : 파일이나 폴더에서 오른쪽 클릭 후 팝업 메뉴
  • Context.sublime-menu : 에디터에서 오른쪽 클릭 후 팝업 메뉴

 

[
{
    "id""edit",
    "children":
    [
        {"id""wrap"},
        "command""prefixr" }
    ]
}
]


: