Skip to content

Commit

Permalink
Merge pull request #2 from brokenprogrammer/feature/paragraph-select
Browse files Browse the repository at this point in the history
Feature/paragraph select
  • Loading branch information
brokenprogrammer authored Nov 11, 2020
2 parents 6c4d5f8 + 14b1b72 commit 582dfd6
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 24 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

All notable changes to the "paragraphjump" extension will be documented in this file.

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

## [Unreleased]
### Added

- Initial release
- Added support for selecting paragraphs up and down.

## 0.0.1

### Added

- Initial release with only jump up and down functionality.
46 changes: 41 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,53 @@ ParagraphJump allows the user to navigate swiftly between paragraphs.

* `paragraphjump.up`: Move up a paragraph
* `paragraphjump.down`: Move down a paragraph

## Extension Settings

Include if your extension adds any VS Code settings through the `contributes.configuration` extension point.
* `paragraphjump.selectup`: Select one paragraph up
* `paragraphjump.selectdown`: Select one paragraph down

## Gettings started

This plugin will automatically be enabled after using one of the commands specified above.

### Keyboard shortcuts

To optimally use this plugin it is recommended to re-map your keybindings. You can do this through the **Keyboard Shortcuts** editor available
through the **Preferences** -> **Keyboard Shortcuts** menu.

My personal bindings for this plugin is displayed below as an example:

```JSON
{
"key": "ctrl+up",
"command": "paragraphjump.up",
"when": "textInputFocus"
},
{
"key": "ctrl+down",
"command": "paragraphjump.down",
"when": "textInputFocus"
},
{
"key": "ctrl+shift+down",
"command": "paragraphjump.selectdown",
"when": "textInputFocus"
},
{
"key": "ctrl+shift+up",
"command": "paragraphjump.selectup",
"when": "textInputFocus"
}
```

## Known Issues

None at the time.

## Release Notes

### 1.0.0

First full release with all the intended functionality for the plugin.

### 0.0.1

Initial release including move up and down features.
Initial release including move up and down features
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "paragraphjump",
"displayName": "ParagraphJump",
"description": "Allows the user to navigate swiftly between paragraphs",
"version": "0.0.1",
"version": "1.0.0",
"publisher": "brokenprogrammer",
"repository": {
"type": "git",
Expand All @@ -25,7 +25,9 @@
],
"activationEvents": [
"onCommand:paragraphjump.up",
"onCommand:paragraphjump.down"
"onCommand:paragraphjump.down",
"onCommand:paragraphjump.selectup",
"onCommand:paragraphjump.selectdown"
],
"main": "./out/extension.js",
"contributes": {
Expand All @@ -37,6 +39,14 @@
{
"command": "paragraphjump.down",
"title": "ParagraphJump: Down"
},
{
"command": "paragraphjump.selectup",
"title": "ParagraphJump: Select Up"
},
{
"command": "paragraphjump.selectdown",
"title": "ParagraphJump: Select Down"
}
]
},
Expand Down
107 changes: 93 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import * as vscode from "vscode";

enum LineOperation {
up = 0,
down = 1,
}

enum MoveOperation {
move = 0,
select = 1,
}

enum ParagraphJumpOperation {
moveUp = 0,
moveDown = 1,
selectUp = 2,
selectDown = 3,
}

const targetLineIsEmptyOrWhitespace = (
line: number,
document: vscode.TextDocument
) => !document.lineAt(line).isEmptyOrWhitespace;

enum LineOperation {
up = 1,
down = 2,
}

function getNextLine(editor: vscode.TextEditor, op: LineOperation) {
let document = editor.document;
let line = editor.selection.active.line;
Expand All @@ -34,32 +46,99 @@ function getNextLine(editor: vscode.TextEditor, op: LineOperation) {
return document.lineAt(line);
}

function moveCursor(editor: vscode.TextEditor, newPosition: vscode.Position) {
let newCursorPosition = new vscode.Selection(newPosition, newPosition);
function moveCursor(
editor: vscode.TextEditor,
newPosition: vscode.Position,
op: MoveOperation
) {
let newCursorPosition: vscode.Selection;
switch (op) {
case MoveOperation.move:
{
newCursorPosition = new vscode.Selection(newPosition, newPosition);
}
break;
case MoveOperation.select:
{
const anchor = editor.selection.anchor;
newCursorPosition = new vscode.Selection(anchor, newPosition);
}
break;
}
editor.selection = newCursorPosition;
editor.revealRange(new vscode.Range(newPosition, newPosition));
}

function performParagraphJumpOperation(
editor: vscode.TextEditor,
op: ParagraphJumpOperation
) {
switch (op) {
case ParagraphJumpOperation.moveUp:
case ParagraphJumpOperation.selectUp:
{
const targetLine = getNextLine(editor, LineOperation.up);
const newPosition = new vscode.Position(targetLine.lineNumber, 0);
const moveOp =
op === ParagraphJumpOperation.moveUp
? MoveOperation.move
: MoveOperation.select;
moveCursor(editor, newPosition, moveOp);
}
break;
case ParagraphJumpOperation.moveDown:
case ParagraphJumpOperation.selectDown:
{
const targetLine = getNextLine(editor, LineOperation.down);
const newPosition = new vscode.Position(
targetLine.lineNumber,
targetLine.text.length
);
const moveOp =
op === ParagraphJumpOperation.moveDown
? MoveOperation.move
: MoveOperation.select;
moveCursor(editor, newPosition, moveOp);
}
break;
}
}

export function activate(context: vscode.ExtensionContext) {
let paragraphJumpUp = vscode.commands.registerTextEditorCommand(
"paragraphjump.up",
(editor: vscode.TextEditor) => {
let targetLine: vscode.TextLine = getNextLine(editor, LineOperation.up);
const newPosition = new vscode.Position(targetLine.lineNumber, 0);
moveCursor(editor, newPosition);
performParagraphJumpOperation(editor, ParagraphJumpOperation.moveUp);
}
);

let paragraphJumpDown = vscode.commands.registerTextEditorCommand(
"paragraphjump.down",
(editor: vscode.TextEditor) => {
let targetLine: vscode.TextLine = getNextLine(editor, LineOperation.down);
const newPosition = new vscode.Position(targetLine.lineNumber, 0);
moveCursor(editor, newPosition);
performParagraphJumpOperation(editor, ParagraphJumpOperation.moveDown);
}
);

context.subscriptions.push(paragraphJumpUp, paragraphJumpDown);
let paragraphSelectUp = vscode.commands.registerTextEditorCommand(
"paragraphjump.selectup",
(editor: vscode.TextEditor) => {
performParagraphJumpOperation(editor, ParagraphJumpOperation.selectUp);
}
);

let paragraphSelectDown = vscode.commands.registerTextEditorCommand(
"paragraphjump.selectdown",
(editor: vscode.TextEditor) => {
performParagraphJumpOperation(editor, ParagraphJumpOperation.selectDown);
}
);

context.subscriptions.push(
paragraphJumpUp,
paragraphJumpDown,
paragraphSelectUp,
paragraphSelectDown
);
}

export function deactivate() {}

0 comments on commit 582dfd6

Please sign in to comment.