Skip to content

Commit f629c2d

Browse files
committed
add copy, move and search functions
1 parent 4e505cc commit f629c2d

File tree

6 files changed

+1940
-1935
lines changed

6 files changed

+1940
-1935
lines changed

.eslintrc.json

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"rules": {
1212
"@typescript-eslint/naming-convention": "warn",
1313
"@typescript-eslint/semi": "warn",
14-
"curly": "warn",
1514
"eqeqeq": "warn",
1615
"no-throw-literal": "warn",
1716
"semi": "off"

README.md

+10-64
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,16 @@
1-
# selection-manager README
1+
# Move selection to new file
22

3-
This is the README for your extension "selection-manager". After writing up a brief description, we recommend including the following sections.
3+
This extension will allow you to manage selected text.<br>
44

5-
## Features
5+
## Usage
66

7-
Describe specific features of your extension including screenshots of your extension in action. Image paths are relative to this README file.
7+
Selecte text of a file and open the 'editor commands' (ctrl+shift+P / cmd+shift+P) and search for one of the next options:
88

9-
For example if there is an image subfolder under your extension project workspace:
9+
#### Copy Selection
10+
copy the selection in a new file
1011

11-
\!\[feature X\]\(images/feature-x.png\)
12+
#### Move Selection
13+
move the selection in a new file, results in having the original text deleated
1214

13-
> Tip: Many popular extensions utilize animations. This is an excellent way to show off your extension! We recommend short, focused animations that are easy to follow.
14-
15-
## Requirements
16-
17-
If you have any requirements or dependencies, add a section describing those and how to install and configure them.
18-
19-
## Extension Settings
20-
21-
Include if your extension adds any VS Code settings through the `contributes.configuration` extension point.
22-
23-
For example:
24-
25-
This extension contributes the following settings:
26-
27-
* `myExtension.enable`: enable/disable this extension
28-
* `myExtension.thing`: set to `blah` to do something
29-
30-
## Known Issues
31-
32-
Calling out known issues can help limit users opening duplicate issues against your extension.
33-
34-
## Release Notes
35-
36-
Users appreciate release notes as you update your extension.
37-
38-
### 1.0.0
39-
40-
Initial release of ...
41-
42-
### 1.0.1
43-
44-
Fixed issue #.
45-
46-
### 1.1.0
47-
48-
Added features X, Y, and Z.
49-
50-
-----------------------------------------------------------------------------------------------------------
51-
## Following extension guidelines
52-
53-
Ensure that you've read through the extensions guidelines and follow the best practices for creating your extension.
54-
55-
* [Extension Guidelines](https://code.visualstudio.com/api/references/extension-guidelines)
56-
57-
## Working with Markdown
58-
59-
**Note:** You can author your README using Visual Studio Code. Here are some useful editor keyboard shortcuts:
60-
61-
* Split the editor (`Cmd+\` on macOS or `Ctrl+\` on Windows and Linux)
62-
* Toggle preview (`Shift+CMD+V` on macOS or `Shift+Ctrl+V` on Windows and Linux)
63-
* Press `Ctrl+Space` (Windows, Linux) or `Cmd+Space` (macOS) to see a list of Markdown snippets
64-
65-
### For more information
66-
67-
* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
68-
* [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/)
69-
70-
**Enjoy!**
15+
#### Search Selection
16+
search for the selection on google

package.json

+60-48
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,63 @@
11
{
22
"name": "selection-manager",
3-
"displayName": "Selection Manager",
4-
"description": "Manage selected text of a file.",
5-
"version": "0.0.1",
6-
"engines": {
7-
"vscode": "^1.66.0"
8-
},
9-
"categories": [
10-
"Other"
11-
],
12-
"activationEvents": [
13-
"onCommand:selection-manager.helloWorld"
14-
],
15-
"main": "./dist/extension.js",
16-
"contributes": {
17-
"commands": [
18-
{
19-
"command": "selection-manager.helloWorld",
20-
"title": "Hello World"
21-
}
22-
]
23-
},
24-
"scripts": {
25-
"vscode:prepublish": "yarn run package",
26-
"compile": "webpack",
27-
"watch": "webpack --watch",
28-
"package": "webpack --mode production --devtool hidden-source-map",
29-
"compile-tests": "tsc -p . --outDir out",
30-
"watch-tests": "tsc -p . -w --outDir out",
31-
"pretest": "yarn run compile-tests && yarn run compile && yarn run lint",
32-
"lint": "eslint src --ext ts",
33-
"test": "node ./out/test/runTest.js"
34-
},
35-
"devDependencies": {
36-
"@types/vscode": "^1.66.0",
37-
"@types/glob": "^7.2.0",
38-
"@types/mocha": "^9.1.0",
39-
"@types/node": "14.x",
40-
"@typescript-eslint/eslint-plugin": "^5.16.0",
41-
"@typescript-eslint/parser": "^5.16.0",
42-
"eslint": "^8.11.0",
43-
"glob": "^7.2.0",
44-
"mocha": "^9.2.2",
45-
"typescript": "^4.5.5",
46-
"ts-loader": "^9.2.8",
47-
"webpack": "^5.70.0",
48-
"webpack-cli": "^4.9.2",
49-
"@vscode/test-electron": "^2.1.3"
50-
}
3+
"displayName": "Selection Manager",
4+
"description": "Manage selected text of a file.",
5+
"version": "0.0.2",
6+
"license": "MIT",
7+
"engines": {
8+
"vscode": "^1.66.0"
9+
},
10+
"categories": [
11+
"Other"
12+
],
13+
"activationEvents": [
14+
"onCommand:selection-manager.copy",
15+
"onCommand:selection-manager.move",
16+
"onCommand:selection-manager.search"
17+
],
18+
"main": "./dist/extension.js",
19+
"contributes": {
20+
"commands": [
21+
{
22+
"command": "selection-manager.copy",
23+
"title": "Copy Selection"
24+
},
25+
{
26+
"command": "selection-manager.move",
27+
"title": "Move Selection"
28+
},
29+
{
30+
"command": "selection-manager.search",
31+
"title": "Search Selection"
32+
}
33+
]
34+
},
35+
"scripts": {
36+
"vscode:prepublish": "yarn run package",
37+
"compile": "webpack",
38+
"watch": "webpack --watch",
39+
"package": "webpack --mode production --devtool hidden-source-map",
40+
"compile-tests": "tsc -p . --outDir out",
41+
"watch-tests": "tsc -p . -w --outDir out",
42+
"pretest": "yarn run compile-tests && yarn run compile && yarn run lint",
43+
"lint": "eslint src --ext ts",
44+
"test": "node ./out/test/runTest.js"
45+
},
46+
"devDependencies": {
47+
"@types/glob": "^7.2.0",
48+
"@types/mocha": "^9.1.0",
49+
"@types/node": "14.x",
50+
"@types/vscode": "^1.66.0",
51+
"@typescript-eslint/eslint-plugin": "^5.16.0",
52+
"@typescript-eslint/parser": "^5.16.0",
53+
"@vscode/test-electron": "^2.1.3",
54+
"eslint": "^8.11.0",
55+
"glob": "^7.2.0",
56+
"mocha": "^9.2.2",
57+
"ts-loader": "^9.2.8",
58+
"typescript": "^4.5.5",
59+
"webpack": "^5.70.0",
60+
"webpack-cli": "^4.9.2"
61+
},
62+
"publisher": "ManageTextSelection"
5163
}

src/extension.ts

+62-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,71 @@
1-
// The module 'vscode' contains the VS Code extensibility API
2-
// Import the module and reference it with the alias vscode in your code below
31
import * as vscode from 'vscode';
2+
import { range } from './utils';
3+
4+
async function copyManager(): Promise<void> {
5+
const editor = vscode.window.activeTextEditor;
6+
7+
if (!editor) return;
8+
9+
const selectedText = editor.document.getText(editor.selection);
10+
11+
const document = await vscode.workspace.openTextDocument({
12+
language: editor.document.languageId,
13+
content: selectedText
14+
});
15+
16+
vscode.window.showTextDocument(document, undefined, true);
17+
}
18+
19+
async function moveManager(): Promise<void> {
20+
const editor = vscode.window.activeTextEditor;
21+
22+
if (!editor) return;
23+
24+
const selectedText = editor.document.getText(editor.selection);
25+
26+
const selectionStartIndex = editor.selection.start.line;
27+
const selectionEndIndex = editor.selection.end.line;
28+
29+
editor.edit(edit => {
30+
range(selectionStartIndex, selectionEndIndex).forEach(index => {
31+
const line = editor.document.lineAt(index);
32+
edit.delete(line.rangeIncludingLineBreak);
33+
});
34+
});
35+
36+
const document = await vscode.workspace.openTextDocument({
37+
language: editor.document.languageId,
38+
content: selectedText
39+
});
40+
41+
vscode.window.showTextDocument(document, undefined, true);
42+
}
43+
44+
async function searchManager(): Promise<void> {
45+
const editor = vscode.window.activeTextEditor;
46+
47+
if (!editor) return;
48+
49+
const selectedText = editor.document.getText(editor.selection);
50+
51+
const uri = vscode.Uri.from({
52+
path: 'search',
53+
query: `q=${selectedText}`,
54+
authority: 'google.com',
55+
scheme: 'https'
56+
});
57+
58+
vscode.env.openExternal(uri);
59+
}
460

5-
// this method is called when your extension is activated
6-
// your extension is activated the very first time the command is executed
761
export function activate(context: vscode.ExtensionContext) {
8-
9-
// Use the console to output diagnostic information (console.log) and errors (console.error)
10-
// This line of code will only be executed once when your extension is activated
1162
console.log('Congratulations, your extension "selection-manager" is now active!');
1263

13-
// The command has been defined in the package.json file
14-
// Now provide the implementation of the command with registerCommand
15-
// The commandId parameter must match the command field in package.json
16-
let disposable = vscode.commands.registerCommand('selection-manager.helloWorld', () => {
17-
// The code you place here will be executed every time your command is executed
18-
// Display a message box to the user
19-
vscode.window.showInformationMessage('Hello World from Selection Manager!');
20-
});
64+
let dispoeCopyManager = vscode.commands.registerCommand('selection-manager.copy', copyManager);
65+
let dispoeMoveManager = vscode.commands.registerCommand('selection-manager.move', moveManager);
66+
let dispoeSearchManager = vscode.commands.registerCommand('selection-manager.search', searchManager);
2167

22-
context.subscriptions.push(disposable);
68+
context.subscriptions.push(dispoeCopyManager, dispoeMoveManager, dispoeSearchManager);
2369
}
2470

25-
// this method is called when your extension is deactivated
2671
export function deactivate() {}

src/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const range = (start: number, end: number): number[] => {
2+
return Array(end - start + 1).fill(null).map((_, idx) => start + idx);
3+
};

0 commit comments

Comments
 (0)