blog icon indicating copy to clipboard operation
blog copied to clipboard

vs code 插件开发

Open huruji opened this issue 4 years ago • 0 comments

使用案例

将选中的文本替换

选中文本转化为大写:

import * as vscode from 'vscode'

import { Range, commands } from 'vscode'

export function activate(context: vscode.ExtensionContext) {
  console.log('active')
  const disposable = commands.registerTextEditorCommand('uppercase.toUpperCase', toUpperCase)
  context.subscriptions.push(disposable)
}

function toUpperCase(editor: vscode.TextEditor) {
  editor.edit(builder => {
    editor.selections.forEach(selection => {
      const range = new Range(selection.start, selection.end)
      const text = editor.document.getText(range) || ''
      builder.replace(selection, text.toLocaleUpperCase())
    })
  })
}

export function deactivate() {
  return
}

参考:https://github.com/ruiquelhas/vscode-uppercase

选中 hover 效果

export function activate(context: vscode.ExtensionContext) {
  const hover = languages.registerHoverProvider({
    scheme: '*',
    language: '*',
  }, {
    provideHover(document, position, token) {
      return new Hover('hello world')
    }
  })

  context.subscriptions.push(hover)
}

参考:https://github.com/thegtproject/vscode-hoverhex

huruji avatar May 05 '20 09:05 huruji