Skip to content

Commit f8ebac6

Browse files
committed
move to new file
0 parents  commit f8ebac6

19 files changed

+2307
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
out
2+
dist
3+
node_modules
4+
.vscode-test/
5+
*.vsix

.vscode-test.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from '@vscode/test-cli';
2+
3+
export default defineConfig({
4+
files: 'out/test/**/*.test.js',
5+
});

.vscode/extensions.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"dbaeumer.vscode-eslint",
6+
"ms-vscode.extension-test-runner"
7+
]
8+
}

.vscode/launch.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// A launch configuration that compiles the extension and then opens it inside a new window
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
{
6+
"version": "0.2.0",
7+
"configurations": [
8+
{
9+
"name": "Run Extension",
10+
"type": "extensionHost",
11+
"request": "launch",
12+
"args": [
13+
"--extensionDevelopmentPath=${workspaceFolder}"
14+
],
15+
"outFiles": [
16+
"${workspaceFolder}/out/**/*.js"
17+
],
18+
"preLaunchTask": "${defaultBuildTask}"
19+
}
20+
]
21+
}

.vscode/settings.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"files.exclude": {
4+
"out": false // set this to true to hide the "out" folder with the compiled JS files
5+
},
6+
"search.exclude": {
7+
"out": true // set this to false to include "out" folder in search results
8+
},
9+
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
10+
"typescript.tsc.autoDetect": "off"
11+
}

.vscode/tasks.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// See https://go.microsoft.com/fwlink/?LinkId=733558
2+
// for the documentation about the tasks.json format
3+
{
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"type": "npm",
8+
"script": "watch",
9+
"problemMatcher": "$tsc-watch",
10+
"isBackground": true,
11+
"presentation": {
12+
"reveal": "never"
13+
},
14+
"group": {
15+
"kind": "build",
16+
"isDefault": true
17+
}
18+
}
19+
]
20+
}

.vscodeignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.vscode/**
2+
.vscode-test/**
3+
src/**
4+
.gitignore
5+
.yarnrc
6+
vsc-extension-quickstart.md
7+
**/tsconfig.json
8+
**/eslint.config.mjs
9+
**/*.map
10+
**/*.ts
11+
**/.vscode-test.*

.yarnrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--ignore-engines true

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Change Log
2+
3+
All notable changes to the "gohelp" extension will be documented in this file.
4+
5+
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
6+
7+
## [Unreleased]
8+
9+
- Initial release

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# gohelp README
2+
3+
This is the README for your extension "gohelp". After writing up a brief description, we recommend including the following sections.
4+
5+
## Features
6+
7+
Describe specific features of your extension including screenshots of your extension in action. Image paths are relative to this README file.
8+
9+
For example if there is an image subfolder under your extension project workspace:
10+
11+
\!\[feature X\]\(images/feature-x.png\)
12+
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+
52+
## Following extension guidelines
53+
54+
Ensure that you've read through the extensions guidelines and follow the best practices for creating your extension.
55+
56+
* [Extension Guidelines](https://code.visualstudio.com/api/references/extension-guidelines)
57+
58+
## Working with Markdown
59+
60+
You can author your README using Visual Studio Code. Here are some useful editor keyboard shortcuts:
61+
62+
* Split the editor (`Cmd+\` on macOS or `Ctrl+\` on Windows and Linux).
63+
* Toggle preview (`Shift+Cmd+V` on macOS or `Shift+Ctrl+V` on Windows and Linux).
64+
* Press `Ctrl+Space` (Windows, Linux, macOS) to see a list of Markdown snippets.
65+
66+
## For more information
67+
68+
* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
69+
* [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/)
70+
71+
**Enjoy!**

eslint.config.mjs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import typescriptEslint from "@typescript-eslint/eslint-plugin";
2+
import tsParser from "@typescript-eslint/parser";
3+
4+
export default [{
5+
files: ["**/*.ts"],
6+
}, {
7+
plugins: {
8+
"@typescript-eslint": typescriptEslint,
9+
},
10+
11+
languageOptions: {
12+
parser: tsParser,
13+
ecmaVersion: 2022,
14+
sourceType: "module",
15+
},
16+
17+
rules: {
18+
"@typescript-eslint/naming-convention": ["warn", {
19+
selector: "import",
20+
format: ["camelCase", "PascalCase"],
21+
}],
22+
23+
curly: "warn",
24+
eqeqeq: "warn",
25+
"no-throw-literal": "warn",
26+
semi: "warn",
27+
},
28+
}];

package.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "gohelp",
3+
"displayName": "gohelp",
4+
"description": "",
5+
"version": "0.0.1",
6+
"engines": {
7+
"vscode": "^1.92.0"
8+
},
9+
"categories": [
10+
"Other"
11+
],
12+
"activationEvents": [
13+
"onLanguage:go"
14+
],
15+
"contributes": {
16+
"commands": [
17+
{
18+
"command": "gohelp.moveToNewFile",
19+
"title": "Move to New File"
20+
}
21+
]
22+
},
23+
"main": "./out/extension.js",
24+
"scripts": {
25+
"vscode:prepublish": "yarn run compile",
26+
"compile": "tsc -p ./",
27+
"watch": "tsc -watch -p ./",
28+
"pretest": "yarn run compile && yarn run lint",
29+
"lint": "eslint src",
30+
"test": "vscode-test"
31+
},
32+
"devDependencies": {
33+
"@types/vscode": "^1.92.0",
34+
"@types/mocha": "^10.0.7",
35+
"@types/node": "20.x",
36+
"@typescript-eslint/eslint-plugin": "^8.3.0",
37+
"@typescript-eslint/parser": "^8.3.0",
38+
"eslint": "^9.9.1",
39+
"typescript": "^5.5.4",
40+
"@vscode/test-cli": "^0.0.10",
41+
"@vscode/test-electron": "^2.4.1"
42+
}
43+
}

src/MoveToNewFile.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import path from "path";
2+
import * as vscode from "vscode";
3+
import Utils from "./utils";
4+
5+
export default class GoMoveToNewFileActionProvider
6+
implements vscode.CodeActionProvider
7+
{
8+
provideCodeActions(
9+
document: vscode.TextDocument,
10+
range: vscode.Range
11+
): vscode.CodeAction[] {
12+
const lineText = document.lineAt(range.start.line).text;
13+
const typeOrMethodRegex = /^(type|func)\s+(\w+)/;
14+
const match = lineText.match(typeOrMethodRegex);
15+
16+
if (match) {
17+
const action = new vscode.CodeAction(
18+
"Move to new file",
19+
vscode.CodeActionKind.RefactorMove
20+
);
21+
action.command = {
22+
title: "Move to new file",
23+
command: "gohelp.moveToNewFile",
24+
arguments: [document, range, match[2]],
25+
};
26+
return [action];
27+
}
28+
29+
return [];
30+
}
31+
}
32+
33+
vscode.commands.registerCommand(
34+
"gohelp.moveToNewFile",
35+
async (document: vscode.TextDocument, range: vscode.Range, name: string) => {
36+
const originalDir = path.dirname(document.uri.fsPath);
37+
const originalPackageName = await Utils.getPackageName(document);
38+
const newFilePath = await Utils.promptForNewFilePath(document, name);
39+
const newFileDir = newFilePath ? path.dirname(newFilePath) : "";
40+
41+
if (newFilePath) {
42+
let newPackageName =
43+
originalDir === newFileDir
44+
? originalPackageName
45+
: path.basename(path.dirname(newFilePath));
46+
const content = await Utils.extractContent(
47+
document,
48+
range.start.line,
49+
name
50+
);
51+
if (!content) {
52+
vscode.window.showErrorMessage(
53+
"Failed to extract content. Please try again."
54+
);
55+
return;
56+
}
57+
const updatedContent = Utils.updatePackageAndImports(
58+
content,
59+
originalPackageName,
60+
newPackageName
61+
);
62+
await Utils.createNewFileWithContent(
63+
newFilePath,
64+
newPackageName,
65+
updatedContent
66+
);
67+
await Utils.removeContentFromOriginalFile(document, content);
68+
await Utils.updateImportsInOriginalFile(document, name, newPackageName);
69+
await vscode.workspace
70+
.openTextDocument(newFilePath)
71+
.then((doc) => vscode.window.showTextDocument(doc));
72+
}
73+
}
74+
);

src/extension.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as vscode from "vscode";
2+
import GoMoveToNewFileActionProvider from "./MoveToNewFile";
3+
4+
export function activate(context: vscode.ExtensionContext) {
5+
console.log("GoHelper ext is now active!");
6+
7+
const disposable = vscode.languages.registerCodeActionsProvider(
8+
{ scheme: "file", language: "go" },
9+
new GoMoveToNewFileActionProvider(),
10+
{
11+
providedCodeActionKinds: [vscode.CodeActionKind.RefactorMove],
12+
}
13+
);
14+
15+
context.subscriptions.push(disposable);
16+
}
17+
18+
export function deactivate() {}

src/test/extension.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as assert from 'assert';
2+
3+
// You can import and use all API from the 'vscode' module
4+
// as well as import your extension to test it
5+
import * as vscode from 'vscode';
6+
// import * as myExtension from '../../extension';
7+
8+
suite('Extension Test Suite', () => {
9+
vscode.window.showInformationMessage('Start all tests.');
10+
11+
test('Sample test', () => {
12+
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
13+
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
14+
});
15+
});

0 commit comments

Comments
 (0)