Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

[![Build Status](https://travis-ci.org/markvincze/vscode-codeFragments.svg?branch=master)](https://travis-ci.org/markvincze/vscode-codeFragments)

With this extension you can save static code fragments for later use, and easily insert them into source files whenever needed.
With this extension you can save static code fragments for later use, and easily insert them into source files whenever needed.
It can be specifically useful when doing coding demos during presentations.

## Features

After installing the extension, a new section called `CODE FRAGMENTS` is added to the Explorer, which shows a list of the previously saved fragments.
You can save a piece of code by selecting it in the editor, and then executing the "Save selection as Code Fragment" from the Command Palette (brought up with `Ctrl+Shift+P`) or in the context menu.
After installing the extension, a new section called `CODE FRAGMENTS` is added to the Explorer, which shows a list of the previously saved fragments.
You can save a piece of code by selecting it in the editor, and then executing the "Save selection as Code Fragment" from the Command Palette (brought up with `Ctrl+Shift+P`) or in the context menu.
Clicking on an existing fragment in the list inserts its content into the editor at the current cursor position.

Saving a selection as a Code Fragment:
Expand All @@ -19,8 +19,16 @@ Inserting a Code Fragment to the current cursor position:

![Inserting Code Fragments.](images/codefragments-insert.gif)

Inserting a Code Fragment with typewriter effect:
![Typewriter Effect](images/typewriter-effect.gif)

![Typewriter Effect Settings](images/typewriter-settings.png)

## Release Notes

### 1.3.0?

Add the capability to always ask for the name of the fragment on save (controlled by the `codeFragments.enableTypewriterEffect` setting).
### 1.2.0

Add the capability to always ask for the name of the fragment on save (controlled by the `codeFragments.askForNameOnCreate` setting) (#5).
Expand All @@ -44,5 +52,5 @@ The icons are licensed under <a href="http://creativecommons.org/licenses/by/3.0
- <a href="http://www.freepik.com" title="Freepik">Freepik</a>
- <a href="https://www.flaticon.com/authors/kirill-kazachek" title="Kirill Kazachek">Kirill Kazachek</a>
- <a href="https://www.flaticon.com/authors/icon-works" title="Icon Works">Icon Works</a>

Downloaded from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a>.
Binary file added images/typewriter-effect.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/typewriter-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
"type": "boolean",
"default": false,
"description": "Prompt for specifying a name when creating a new fragment."
},
"codeFragments.enableTypewriterEffect": {
"type": "boolean",
"default": false,
"description": "Enable/disable typewriter effect"
}
}
}
Expand Down Expand Up @@ -203,4 +208,4 @@
"typescript": "^2.6.1",
"vscode": "^1.1.8"
}
}
}
3 changes: 1 addition & 2 deletions src/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ export class Exporter {
if (err) {
reject(err);
}

resolve();
resolve(null);
});
});
}
Expand Down
21 changes: 18 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { CodeFragmentProvider, CodeFragmentTreeItem } from './codeFragmentsTreeI
import { Exporter, ImportResult } from './exporter';
import { FragmentManager } from './fragmentManager';

function wait(milliseconds: number) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}

export async function activate(context: vscode.ExtensionContext) {
const fragmentManager = new FragmentManager(context);
const codeFragmentProvider = new CodeFragmentProvider(fragmentManager);
Expand Down Expand Up @@ -63,9 +67,20 @@ export async function activate(context: vscode.ExtensionContext) {
const content = fragmentManager.getFragmentContent(fragmentId);

if (content) {
editor.edit(builder => {
builder.insert(editor.selection.start, content.content);
});
const config = vscode.workspace.getConfiguration('codeFragments');
if (!config.get('enableTypewriterEffect')) {
editor.edit(builder => {
builder.insert(editor.selection.start, content.content);
});
} else {
let line = editor.selection.start.line;
let start = editor.selection.start.character;
[...content.content].reduce((promise: Promise<any>, character: string, index: number) =>
promise.then((_: any) => editor.edit(editBuilder => editBuilder.insert(new vscode.Position(line, start + index), character))
.then(_ => { if (character === '\n') { line++ } })
.then(_ => { return wait(100) })
), Promise.resolve());
}
}
};

Expand Down