Skip to content

Commit

Permalink
Merge pull request #3 from msamgan/v0.0.7
Browse files Browse the repository at this point in the history
format individual file.
  • Loading branch information
msamgan authored Jun 28, 2022
2 parents 3130fa7 + c48bd65 commit 5558964
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 29 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

vscode extension for laravel pint. For details follow the official documentation of [laravel/pint](https://github.com/laravel/pint)

Just install and get started by opening command pallet by `Ctrl+Shift+P on Windows (Cmd+Shift+P on Mac OS)` and typing `Format with laravel pint`

This will **format the entire project** with the set configurations you provided.
Just install and get started.

**NO ADDITION CONFIGURATION REQUIRED**

Expand All @@ -16,4 +14,20 @@ ext install msamgan.laravel-pint-vscode

or you can also install by searching "pint" in vscode extensions and selecting "Laravel Pint Formatter" from "Mohammed Samgan khan"

## Usage

Open command pallet by `Ctrl+Shift+P on Windows (Cmd+Shift+P on Mac OS)` and type `laravel pint`

## Commands

Below are the all available commands.

### Laravel Pint: Format project

This will **format the entire project** with the set configurations you provided.

### Laravel Pint: Format current file

This will **format the current open file** with the set configurations you provided.

**Enjoy!**
60 changes: 38 additions & 22 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require("vscode")
const cp = require("child_process")
const fs = require("fs")
const path = require("path")

const PINT_BINARY = "vendor/bin/pint"

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
const {
checkBinaryExist,
projectDirectory,
infoMessage,
errorMessage,
currentEditor
} = require("./utils/methods")
const { PINT_BINARY } = require("./utils/constants")

/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand("laravel-pint-vscode.format", async () => {
const projectDirectory = vscode.workspace.workspaceFolders[0].uri.path
let binaryExist = fs.existsSync(path.join(projectDirectory, PINT_BINARY))

if (!binaryExist) {
return vscode.window.showErrorMessage(
"Executable not readable or lacks permissions or do not exist - Laravel Pint."
)
let format = vscode.commands.registerCommand("laravel-pint-vscode.format", async () => {
if (!checkBinaryExist()) {
return errorMessage("Pint binary not found. Please install it first.")
}

cp.exec(
PINT_BINARY,
{
cwd: projectDirectory
cwd: projectDirectory()
},
// eslint-disable-next-line no-unused-vars
(err, stdout, stderr) => {
if (err) {
return errorMessage("Something went wrong while running Laravel Pint.")
}
}
)

return infoMessage("Formatting your project with Laravel Pint.")
})

context.subscriptions.push(format)

let formatFile = vscode.commands.registerCommand("laravel-pint-vscode.format-file", async () => {
if (!checkBinaryExist()) {
return errorMessage("Pint binary not found. Please install it first.")
}

cp.exec(
PINT_BINARY + " " + currentEditor(),
{
cwd: projectDirectory()
},
// eslint-disable-next-line no-unused-vars
(err, stdout, stderr) => {
// console.log("stdout: " + stdout)
if (err) {
return vscode.window.showErrorMessage("Something went wrong while running Laravel Pint.")
return errorMessage("Something went wrong while running Laravel Pint.")
}
}
)

vscode.window.showInformationMessage("Formatting your code with Laravel Pint.")
return infoMessage("Formatting your current file with Laravel Pint.")
})

context.subscriptions.push(disposable)
context.subscriptions.push(formatFile)
}

// this method is called when your extension is deactivated
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "laravel-pint-vscode",
"displayName": "Laravel Pint Formatter",
"description": "vscode extension for laravel pint",
"description": "vscode extension for laravel pint with zero config",
"publisher": "msamgan",
"version": "0.0.5",
"version": "0.0.7",
"icon": "larapint-icon.png",
"engines": {
"vscode": "^1.68.0"
Expand Down Expand Up @@ -32,14 +32,19 @@
"url": "https://github.com/msamgan/vscode-larapint"
},
"activationEvents": [
"onCommand:laravel-pint-vscode.format"
"onCommand:laravel-pint-vscode.format",
"onCommand:laravel-pint-vscode.format-file"
],
"main": "./extension.js",
"contributes": {
"commands": [
{
"command": "laravel-pint-vscode.format",
"title": "Format with laravel pint"
"title": "Laravel Pint: Format project"
},
{
"command": "laravel-pint-vscode.format-file",
"title": "Laravel Pint: Format current file"
}
]
},
Expand Down
3 changes: 3 additions & 0 deletions utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
PINT_BINARY: "vendor/bin/pint"
}
46 changes: 46 additions & 0 deletions utils/methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const vscode = require("vscode")
const { PINT_BINARY } = require("./constants")
const fs = require("fs")
const path = require("path")

/**
* Returns the path to the project directory.
* @returns {string} The path to the project directory.
*/
exports.projectDirectory = () => {
return vscode.workspace.workspaceFolders[0].uri.path
}

/**
* Returns the current editor's file name.
* @returns {string} The current editor's file name.
*/
exports.currentEditor = () => {
return vscode.window.activeTextEditor.document.fileName
}

/**
* Checks if the pint binary is installed on the system.
* @returns {boolean} - true if the binary is installed, false otherwise.
*/
exports.checkBinaryExist = () => {
return fs.existsSync(path.join(this.projectDirectory(), PINT_BINARY)) ? true : false
}

/**
* Displays a message to the user.
* @param {string} message - the message to display
* @returns None
*/
exports.infoMessage = (message) => {
return vscode.window.showInformationMessage(message)
}

/**
* Displays an error message to the user.
* @param {string} message - the message to display to the user
* @returns None
*/
exports.errorMessage = (message) => {
return vscode.window.showErrorMessage(message)
}

0 comments on commit 5558964

Please sign in to comment.