插件代码片段
获取光标位置
ts
const editor = window.activeTextEditor
if (!editor) return // 不一定有编辑器打开,所以需要判断一下
const cursorPosition = editor.selection
const editor = window.activeTextEditor
if (!editor) return // 不一定有编辑器打开,所以需要判断一下
const cursorPosition = editor.selection
获取光标左侧文本
ts
const lineText = editor.document.lineAt(cursorPosition.start.line).text
const lineTextList = lineText.trim().split(" ")
const lastWordBeforeCursor = lineTextList[lineTextList.length - 1]
const lineText = editor.document.lineAt(cursorPosition.start.line).text
const lineTextList = lineText.trim().split(" ")
const lastWordBeforeCursor = lineTextList[lineTextList.length - 1]
在光标位置后面插入文本
ts
editor.edit(editBuilder => {
editBuilder.insert(editor.document.lineAt(cursorPosition.active).range.end, ": ;")
})
editor.edit(editBuilder => {
editBuilder.insert(editor.document.lineAt(cursorPosition.active).range.end, ": ;")
})
更改当前编辑器全部文本
ts
editorBuilder.replace(
new Range(new Position(0, 0), new Position(activeDocument.lineCount + 1, 0)),
Str
)
editorBuilder.replace(
new Range(new Position(0, 0), new Position(activeDocument.lineCount + 1, 0)),
Str
)
路径跳转
ts
import { window, TextDocument, Position, Location, Uri, languages, ExtensionContext } from "vscode"
import { dirname } from "path"
import { existsSync } from "fs"
// vscode路径跳转插件的机制是:
// 1.使用languages.registerDefinitionProvider定义一个Provider(处理路径的函数)
// 2.当按住Ctrl键时,如果该函数return了一个location,字符串就会变成一个可以点击的链接,vscode就会跳转该路径
/**
* @param {*} document 当前打开的编辑器
* @param {*} position 光标所在位置
*/
function provideDefinition(document: TextDocument, position: Position) {
const fileName = document.fileName
// console.log(" 当前文件完整路径", fileName)
const workDir = dirname(fileName)
// console.log(" 当前文件所在目录", workDir)
const word = document.getText(document.getWordRangeAtPosition(position))
// console.log(" 当前光标所在单词", word)
const line = document.lineAt(position)
// console.log("当前光标所在行", line)
if (/package\.json$/.test(fileName)) {
let jumpPath = `${workDir}\\node_modules\\${word
.replace(/"/g, "")
.replace(/\//g, "\\")}\\package.json`
// console.log("node_modules路径", destPath)
// console.log("是否存在该包", existsSync(destPath))
if (existsSync(jumpPath)) {
// new Position(0, 0) 表示位置为第一行第一列
return new Location(Uri.file(jumpPath), new Position(0, 0))
} else {
window.showInformationMessage("info!")
}
}
}
/**
* json文件中触发路径跳转插件
* @param context vscode扩展上下文
*/
export function activate(context: ExtensionContext) {
context.subscriptions.push(
languages.registerDefinitionProvider(["json"], {
provideDefinition
})
)
}
import { window, TextDocument, Position, Location, Uri, languages, ExtensionContext } from "vscode"
import { dirname } from "path"
import { existsSync } from "fs"
// vscode路径跳转插件的机制是:
// 1.使用languages.registerDefinitionProvider定义一个Provider(处理路径的函数)
// 2.当按住Ctrl键时,如果该函数return了一个location,字符串就会变成一个可以点击的链接,vscode就会跳转该路径
/**
* @param {*} document 当前打开的编辑器
* @param {*} position 光标所在位置
*/
function provideDefinition(document: TextDocument, position: Position) {
const fileName = document.fileName
// console.log(" 当前文件完整路径", fileName)
const workDir = dirname(fileName)
// console.log(" 当前文件所在目录", workDir)
const word = document.getText(document.getWordRangeAtPosition(position))
// console.log(" 当前光标所在单词", word)
const line = document.lineAt(position)
// console.log("当前光标所在行", line)
if (/package\.json$/.test(fileName)) {
let jumpPath = `${workDir}\\node_modules\\${word
.replace(/"/g, "")
.replace(/\//g, "\\")}\\package.json`
// console.log("node_modules路径", destPath)
// console.log("是否存在该包", existsSync(destPath))
if (existsSync(jumpPath)) {
// new Position(0, 0) 表示位置为第一行第一列
return new Location(Uri.file(jumpPath), new Position(0, 0))
} else {
window.showInformationMessage("info!")
}
}
}
/**
* json文件中触发路径跳转插件
* @param context vscode扩展上下文
*/
export function activate(context: ExtensionContext) {
context.subscriptions.push(
languages.registerDefinitionProvider(["json"], {
provideDefinition
})
)
}