Skip to content

Commit

Permalink
Update to 0.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkenefick committed Feb 21, 2022
1 parent 62bd33f commit 13547bf
Show file tree
Hide file tree
Showing 17 changed files with 1,221 additions and 247 deletions.
16 changes: 8 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"root": true,
"ignorePatterns": [
"out",
"dist",
"**/*.d.ts"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
Expand All @@ -8,17 +12,13 @@
"plugins": [
"@typescript-eslint"
],
"root": true,
"rules": {
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/semi": "warn",
"curly": "warn",
"eqeqeq": "warn",
"no-throw-literal": "warn",
"semi": "off"
},
"ignorePatterns": [
"out",
"dist",
"**/*.d.ts"
]
}
}
}
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to the "box-comment" extension will be documented in this fi

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [0.1.7]

- Ability to uncomment entire blocks back to normal
- Use block comments rather than line blocks
- `box-comment.indentation` setting for pre-indentation
- `box-comment.length` setting for total line length
- `box-comment.linebreak.before` boolean flag
- `box-comment.linebreak.after` boolean flag

## [Unreleased]

- Initial release
- Initial release
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
# Ascii Box Comments

[![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/mattkenefick/vscode-box-comment)
[![Visual Studio Marketplace Version](https://img.shields.io/visual-studio-marketplace/v/polymermallard.box-comment.svg)](https://marketplace.visualstudio.com/items?itemName=polymermallard.box-comment)
[![Visual Studio Marketplace Installs](https://img.shields.io/visual-studio-marketplace/i/polymermallard.box-comment.svg)](https://marketplace.visualstudio.com/items?itemName=polymermallard.box-comment)
[![Backers on Patreon](https://img.shields.io/badge/backer-Patreon-orange.svg)](https://www.patreon.com/mattkenefick)
[![Backers on Paypal](https://img.shields.io/badge/backer-Paypal-blue.svg)](https://paypal.me/polymermallard)

Generates a clean comment box using ascii box drawing characters around a selection.
Inspired by this prototype: https://prototypes.polymermallard.com/docblock/index.html

![Box comment example](./assets/box-comment.gif)


## Example

```PHP
Expand Down
43 changes: 23 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"onCommand:box-comment.createBox120"
],
"author": {
"name": "Matt Kenefick",
"email": "matt@polymermallard.com"
"email": "matt@polymermallard.com",
"name": "Matt Kenefick"
},
"bugs": {
"url": "https://github.com/mattkenefick/vscode-box-comment/issues"
Expand All @@ -29,28 +29,28 @@
"title": "Create comment box (120 wide)"
}
],
"keybindings": [
{
"command": "extension.createBox",
"key": "ctrl+alt+;",
"mac": "shift+cmd+;"
}
]
"keybindings": [
{
"command": "extension.createBox",
"key": "ctrl+alt+;",
"mac": "shift+cmd+;"
}
]
},
"description": "Allows user to write comments within an ASCII drawn box",
"devDependencies": {
"@types/vscode": "^1.57.0",
"@types/glob": "^7.1.3",
"@types/mocha": "^8.2.2",
"@types/node": "14.x",
"eslint": "^7.27.0",
"@types/vscode": "^1.57.0",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"eslint": "^7.27.0",
"glob": "^7.1.7",
"mocha": "^8.4.0",
"ts-loader": "^9.2.2",
"typescript": "^4.3.2",
"vscode-test": "^1.5.2",
"ts-loader": "^9.2.2",
"webpack": "^5.38.1",
"webpack-cli": "^4.7.0"
},
Expand All @@ -63,7 +63,7 @@
"theme": "light"
},
"homepage": "https://github.com/mattkenefick/vscode-box-comment",
"icon": "assets/logo.png",
"icon": "assets/logo.png",
"license": "MIT",
"main": "./dist/extension.js",
"name": "box-comment",
Expand All @@ -73,15 +73,18 @@
"url": "https://github.com/mattkenefick/vscode-box-comment.git"
},
"scripts": {
"vscode:prepublish": "yarn run package",
"compile": "webpack",
"watch": "webpack --watch",
"lint": "eslint src --ext ts",
"package": "webpack --mode production --devtool hidden-source-map",
"pretest": "yarn run test-compile && yarn run lint",
"test": "node ./out/test/runTest.js",
"test-compile": "tsc -p ./",
"test-watch": "tsc -watch -p ./",
"pretest": "yarn run test-compile && yarn run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
"vscode:prepublish": "yarn run package",
"watch": "webpack --watch"
},
"version": "0.1.2"
}
"version": "0.1.7",
"dependencies": {
"vsce": "^2.6.7"
}
}
29 changes: 29 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as vscode from 'vscode';

// Get settings
const settings = vscode.workspace.getConfiguration('box-comment');

// This can be changed per language, so JS can have less than another
const chars = settings?.chars || {};
const indentAmount: number = settings?.indentation || 2;

export default {
CHAR_BL: chars.bl || '└',
CHAR_BM: chars.bm || '─',
CHAR_BR: chars.br || '┘',
CHAR_DL: chars.dl || '├',
CHAR_DM: chars.dm || '─',
CHAR_DR: chars.dr || '┤',
CHAR_L: chars.l || '│',
CHAR_R: chars.r || '│',
CHAR_TL: chars.tl || '┌',
CHAR_TM: chars.tm || '─',
CHAR_TR: chars.tr || '┐',
TOTAL_LINE_LENGTH: settings?.length || 80,
indent: ' '.repeat(indentAmount),
indentAmount: indentAmount,
linebreak: {
after: settings?.linebreak?.after ? `\n` : '',
before: settings?.linebreak?.before ? `\n` : '',
},
};
157 changes: 8 additions & 149 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,162 +1,22 @@
import * as vscode from 'vscode';
import constants from './constants';
import { insertBox } from './functions/insertBox';

/**
* Extension activated
* Inspired by https://prototypes.polymermallard.com/docblock/index.html
*
* @author Matt Kenefick <polymermallard.com>
*/

/**
* Extension activated
*
* @param ExtensionContract context
* @return void
*/
export function activate(context: vscode.ExtensionContext) {
let disposable;

const settings = vscode.workspace.getConfiguration('box-comment');
const chars = settings?.chars || {};

const CHAR_TL = chars.tl || '┌';
const CHAR_TM = chars.tm || '─';
const CHAR_TR = chars.tr || '┐';
const CHAR_L = chars.l || '│';
const CHAR_R = chars.r || '│';
const CHAR_BL = chars.bl || '└';
const CHAR_BM = chars.bm || '─';
const CHAR_BR = chars.br || '┘';
const CHAR_DL = chars.dl || '├';
const CHAR_DM = chars.dm || '─';
const CHAR_DR = chars.dr || '┤';
const TOTAL_LINE_LENGTH = settings?.length || 80;

/**
* Primary function to create a box within our workspace.
*
* @param number lineLength
* @return void
*/
function insertBox(lineLength: number = 80): void {
const editor = vscode.window.activeTextEditor;
const document: vscode.TextDocument | undefined = editor?.document;
const selection: vscode.Selection | undefined = editor?.selection;
let lines: string[] = [];

// If we have a selection, proceed
if (document && editor && selection) {
lines = parseLines(document, selection, lineLength);

// Replace text
editor.edit((editBuilder: vscode.TextEditorEdit): void => {
let replacementText: string = lines
.map((currentValue: string) => {
// Divider
if (currentValue[0] == CHAR_DM) {
return `${CHAR_DL}${currentValue}${CHAR_DR}`;
}
// Regular line
else {
return `${CHAR_L} ${currentValue} ${CHAR_R}`;
}
})
.reduce((previous: string, current: string) => `${previous}\n${current}`);

// Wrap text with upper and lower dividers
replacementText = [
CHAR_TL + CHAR_TM.repeat(lineLength - 2) + CHAR_TR,
replacementText,
CHAR_BL + CHAR_BM.repeat(lineLength - 2) + CHAR_BR,
].join("\n");

// Replace selection with manipulated text
editBuilder.replace(editor.selection, replacementText);
});

// Comment our box with inherited delimiters
commentSelection();
let disposable;

// Remove selection (next tick)
setTimeout(() => {
const position: vscode.Position = new vscode.Position(selection.end.line + 3, 0);
editor.selection = new vscode.Selection(position, position);
}, 1);
}
}
// region: Commands
// ---------------------------------------------------------------------------

/**
* Creates a native comment of our current selection
*
* @return void
*/
function commentSelection(): void {
vscode.commands.executeCommand('editor.action.addCommentLine');
}

/**
* Parse lines into array
*
* @param TextDocument document
* @param Selection selection
* @return string[]
*/
function parseLines(document: vscode.TextDocument, selection: vscode.Selection, maxLineLength: number = 80): string[] {
const linesOfText: string[] = document.getText(selection).toString().split("\n");
let masterArray: string[] = [];

// Regex to replace older occurences of box characters
const regex: RegExp = new RegExp(` ?[${CHAR_TL}${CHAR_TM}${CHAR_TR}${CHAR_BL}${CHAR_BM}${CHAR_BR}${CHAR_DL}${CHAR_DM}${CHAR_DR}${CHAR_L}${CHAR_R}] ?`, 'g');

// Iterate through list of lines and check if any of them are too long
// then create a new array
for (let i = 0, l = linesOfText.length; i < l; i++) {
const len: number = maxLineLength - 2 - CHAR_L.length - CHAR_R.length;
let lines: string[] = [];
let value: string = linesOfText[i];

// If the line is longer than max allowed, parse into new lines
if (value.length >= len) {
while (value.length) {
let indexOf = value.substr(0, len).lastIndexOf(' ');

if (indexOf === -1) {
indexOf = 9999;
}

lines.push(value.substr(0, indexOf));
value = value.substr(indexOf + 1);
}
}
else {
lines = [value];
}

// Merge into single array
masterArray = masterArray.concat(lines);
}

// Process each line
masterArray.forEach((line: string, index: number) => {
// Change remove old box
line = line.replace(regex, '');

// Create line for new box
if (line.trim() == '--') {
masterArray[index] = CHAR_DM.repeat(maxLineLength - 2); // - 2 because we remove the spaces
}
else {
masterArray[index] = line + ' '.repeat(Math.max(0, maxLineLength - line.length - (4)));
}
});

return masterArray;
}


// region: Commands
// ---------------------------------------------------------------------------

disposable = vscode.commands.registerCommand('box-comment.createBox', () => insertBox(TOTAL_LINE_LENGTH));
disposable = vscode.commands.registerCommand('box-comment.createBox', () => insertBox(constants.TOTAL_LINE_LENGTH));
context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand('box-comment.createBox80', () => insertBox(80));
Expand All @@ -165,8 +25,7 @@ export function activate(context: vscode.ExtensionContext) {
disposable = vscode.commands.registerCommand('box-comment.createBox120', () => insertBox(120));
context.subscriptions.push(disposable);

// endregion: Commands

// endregion: Commands
}

// this method is called when your extension is deactivated
Expand Down
21 changes: 21 additions & 0 deletions src/functions/commentSelection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as vscode from 'vscode';

/**
* Creates a native comment of our current selection
*
* @return void
*/
export function commentSelection(): void {
vscode.commands.executeCommand('editor.action.blockComment');

// vscode.commands.executeCommand('editor.action.addCommentLine');
}

/**
* Removes a native comment of our current selection
*
* @return void
*/
export function uncommentSelection(): void {
vscode.commands.executeCommand('editor.action.blockComment');
}
Loading

0 comments on commit 13547bf

Please sign in to comment.