Merge pull request #54 from badsyntax/snippets

Snippets
This commit is contained in:
Richard Willis 2019-11-18 13:29:06 +01:00 коммит произвёл GitHub
Родитель 67d4b94ae7 3ee8247ff6
Коммит 8d0d1c3c96
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
11 изменённых файлов: 153 добавлений и 68 удалений

4
.github/ISSUE_TEMPLATE/bug_report.md поставляемый
Просмотреть файл

@ -4,7 +4,6 @@ about: Create a report to help us improve
title: ''
labels: bug
assignees: badsyntax
---
**Describe the bug**
@ -20,7 +19,8 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.
**Environment (please complete the following information):**
- OS: [e.g. iOS]
- OS: [e.g. iOS]
**Output from "Gradle Tasks"**
You can find this by clicking on the "Output" panel, then selecting the "Gradle Tasks" channel from the dropdown.

1
.github/ISSUE_TEMPLATE/feature_request.md поставляемый
Просмотреть файл

@ -4,7 +4,6 @@ about: Suggest an idea for this project
title: ''
labels: enhancement
assignees: badsyntax
---
**Is your feature request related to a problem? Please describe.**

Просмотреть файл

@ -9,7 +9,7 @@ Run gradle tasks in VS Code.
## Features
- Run gradle tasks as [VS Code tasks](https://code.visualstudio.com/docs/editor/tasks)
- Run [Gradle tasks](https://gradle.org/) as [VS Code tasks](https://code.visualstudio.com/docs/editor/tasks)
- List & run gradle tasks in the Explorer
- Multi-root workspace folders supported
- Default Groovy/Kotlin & custom build files supported
@ -27,6 +27,12 @@ This extension contributes the following settings:
- `gradle.customBuildFile`: Custom gradle build filename
- `gradle.explorerNestedSubProjects`: Show nested subprojects in the explorer
## Snippets
This extensions provides snippets for the groovy and kotlin build files:
- `cgt`: Create a new gradle task
## Slow Task Provider Warning
[Since vscode v1.40](https://code.visualstudio.com/updates/v1_40#_slow-task-provider-warning), you will start seeing warning notifications when the gradle task provider takes too long.

Просмотреть файл

@ -23,7 +23,8 @@
"vscode": "^1.39.0"
},
"categories": [
"Other"
"Other",
"Snippets"
],
"keywords": [
"java",
@ -42,6 +43,16 @@
],
"main": "./out/extension",
"contributes": {
"snippets": [
{
"language": "groovy",
"path": "./snippets/build.gradle.json"
},
{
"language": "kotlinscript",
"path": "./snippets/build.gradle.kts.json"
}
],
"problemMatchers": [
{
"owner": "gradle",
@ -87,6 +98,14 @@
"command": "gradle.openBuildFile",
"title": "Open Build File"
},
{
"command": "gradle.addTask",
"title": "Add Task",
"icon": {
"light": "resources/light/add.svg",
"dark": "resources/dark/add.svg"
}
},
{
"command": "gradle.refresh",
"title": "Gradle: Refresh Tasks",
@ -102,6 +121,10 @@
"command": "gradle.runTask",
"when": "false"
},
{
"command": "gradle.addTask",
"when": "false"
},
{
"command": "gradle.stopTask",
"when": "false"
@ -127,6 +150,15 @@
"command": "gradle.openBuildFile",
"when": "view == gradle-tree-view && viewItem == buildFile"
},
{
"command": "gradle.addTask",
"when": "view == gradle-tree-view && viewItem == buildFile"
},
{
"command": "gradle.addTask",
"when": "view == gradle-tree-view && viewItem == buildFile",
"group": "inline"
},
{
"command": "gradle.runTask",
"when": "view == gradle-tree-view && viewItem == task",
@ -199,6 +231,10 @@
"fileName": {
"type": "string",
"description": "The filename of the build file that provides the tasks"
},
"description": {
"type": "string",
"description": "Description of the task"
}
}
}
@ -211,9 +247,9 @@
"test": "node ./out/test/runTest.js",
"pretest": "npm run compile",
"lint": "npm run lint:prettier && npm run lint:tslint",
"lint:prettier": "prettier --check \"**/*.{ts,js,json,svg}\"",
"lint:prettier": "prettier --check \"**/*.{ts,js,json,svg,md,yml}\"",
"lint:tslint": "tslint -c tslint.json 'src/**/*.ts'",
"format": "prettier --write '**/*.{ts,js,json,svg}'"
"format": "prettier --write '**/*.{ts,js,json,svg,md,yml}'"
},
"dependencies": {},
"devDependencies": {

9
resources/dark/add.svg Normal file
Просмотреть файл

@ -0,0 +1,9 @@
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M14 7V8H8V14H7V8H1V7H7V1H8V7H14Z" fill="#C5C5C5" />
</svg>

После

Ширина:  |  Высота:  |  Размер: 177 B

9
resources/light/add.svg Normal file
Просмотреть файл

@ -0,0 +1,9 @@
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M14 7V8H8V14H7V8H1V7H7V1H8V7H14Z" fill="#424242" />
</svg>

После

Ширина:  |  Высота:  |  Размер: 177 B

Просмотреть файл

@ -0,0 +1,15 @@
{
"createGradleTask": {
"prefix": "cgt",
"body": [
"tasks.register(\"$1\") {",
"\tgroup='$2'",
"\tdescription='$3'",
"\tdoLast {",
"\t\tprintln 'Hello, World'",
"\t}",
"}"
],
"description": "Create a new Gradle task"
}
}

Просмотреть файл

@ -0,0 +1,15 @@
{
"createGradleTask": {
"prefix": "cgt",
"body": [
"tasks.register(\"$1\") {",
"\tgroup=\"$2\"",
"\tdescription=\"$3\"",
"\tdoLast {",
"\t\tprintln(\"Hello, World\")",
"\t}",
"}"
],
"description": "Create a new Gradle task"
}
}

Просмотреть файл

@ -114,6 +114,13 @@ function registerCommands(
treeDataProvider
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
'gradle.addTask',
treeDataProvider.addTask,
treeDataProvider
)
);
}
}

Просмотреть файл

@ -96,7 +96,8 @@ class GradleTaskTreeItem extends vscode.TreeItem {
context: vscode.ExtensionContext,
folderTreeItem: GradleBuildFileTreeItem,
task: vscode.Task,
label: string
label: string,
description?: string
) {
super(label, vscode.TreeItemCollapsibleState.None);
this.command = {
@ -104,6 +105,7 @@ class GradleTaskTreeItem extends vscode.TreeItem {
command: 'gradle.runTask',
arguments: [this]
};
this.tooltip = description || label;
this.folderTreeItem = folderTreeItem;
this.task = task;
this.execution = getTaskExecution(task);
@ -192,6 +194,22 @@ export class GradleTasksTreeDataProvider
}
}
async addTask(buildFileTreeItem: GradleBuildFileTreeItem) {
const uri = buildFileTreeItem.resourceUri;
if (uri) {
const textEditor = await vscode.window.showTextDocument(
await vscode.workspace.openTextDocument(uri)
);
const position = new vscode.Position(textEditor.document.lineCount, 0);
textEditor.selection = new vscode.Selection(position, position);
vscode.commands.executeCommand('editor.action.insertSnippet', {
name: 'createGradleTask'
});
}
}
refresh(): Thenable<vscode.Task[]> {
invalidateTasksCache();
enableTaskDetection();
@ -333,7 +351,8 @@ export class GradleTasksTreeDataProvider
this.extensionContext,
subProjectBuildFileTreeItem,
task,
task.name.replace(/[^:]+:/, '')
task.name.replace(/[^:]+:/, ''),
task.definition.description
);
subProjectBuildFileTreeItem.addTask(gradleTask);
} else {
@ -341,7 +360,8 @@ export class GradleTasksTreeDataProvider
this.extensionContext,
buildFileTreeItem,
task,
task.name
task.name,
task.definition.description
);
buildFileTreeItem.addTask(gradleTask);
}

Просмотреть файл

@ -9,6 +9,7 @@ import {
getTasksArgs
} from './config';
type StringMap = { [s: string]: string };
let autoDetectOverride: boolean = false;
let cachedTasks: Promise<vscode.Task[]> | undefined = undefined;
@ -19,6 +20,7 @@ export function enableTaskDetection() {
export interface GradleTaskDefinition extends vscode.TaskDefinition {
script: string;
fileName: string;
description: string;
}
export class GradleTaskProvider implements vscode.TaskProvider {
@ -42,39 +44,6 @@ export class GradleTaskProvider implements vscode.TaskProvider {
public async resolveTask(
_task: vscode.Task
): Promise<vscode.Task | undefined> {
const gradleTask = (<any>_task.definition).script;
if (gradleTask) {
const { definition } = <any>_task;
let gradleBuildFileUri: vscode.Uri;
if (
_task.scope === undefined ||
_task.scope === vscode.TaskScope.Global ||
_task.scope === vscode.TaskScope.Workspace
) {
// scope is required to be a WorkspaceFolder for resolveTask
return undefined;
}
if (definition.path) {
gradleBuildFileUri = _task.scope.uri.with({
path: _task.scope.uri.path + '/' + definition.path + 'build.gradle'
});
} else {
gradleBuildFileUri = _task.scope.uri.with({
path: _task.scope.uri.path + '/build.gradle'
});
}
const folder = vscode.workspace.getWorkspaceFolder(gradleBuildFileUri);
if (folder) {
return createTask(
definition,
definition.script,
_task.scope,
gradleBuildFileUri,
await getGradleWrapperCommandFromPath(folder.uri.fsPath)
);
}
return undefined;
}
return undefined;
}
}
@ -178,7 +147,7 @@ async function provideGradleTasksForFolder(
if (!command) {
return emptyTasks;
}
const tasksMap = await getTasks(
const tasksMap: StringMap | undefined = await getTasks(
command,
folder,
statusBarItem,
@ -191,9 +160,15 @@ async function provideGradleTasksForFolder(
const sortedKeys = Object.keys(tasksMap).sort((a, b) => a.localeCompare(b));
const tasks: vscode.Task[] = [];
for (const task of sortedKeys) {
for (const taskName of sortedKeys) {
tasks.push(
await createTask(task, task, folder!, gradleBuildFileUri, command)
await createTask(
taskName,
tasksMap[taskName],
folder!,
gradleBuildFileUri,
command
)
);
}
return tasks;
@ -207,31 +182,12 @@ export function getTaskName(task: string, relativePath: string | undefined) {
}
export async function createTask(
taskDefinitionOrTaskName: GradleTaskDefinition | string,
taskName: string,
taskDescription: string,
folder: vscode.WorkspaceFolder,
gradleBuildFileUri: vscode.Uri,
command: string
): Promise<vscode.Task> {
let definition: GradleTaskDefinition;
let fileName: string = path.basename(gradleBuildFileUri.fsPath);
if (typeof taskDefinitionOrTaskName === 'string') {
if (taskDefinitionOrTaskName.includes(':')) {
const [subProjectName] = taskDefinitionOrTaskName.split(':');
const subProjectPath = path.join(folder.uri.fsPath, subProjectName);
const folderUri = vscode.Uri.file(subProjectPath);
const uri = (await getBuildFilePaths(folderUri))[0];
fileName = path.basename(uri.fsPath);
}
definition = {
type: 'gradle',
script: taskDefinitionOrTaskName,
fileName
};
} else {
definition = taskDefinitionOrTaskName;
}
function getCommandLine(task: string): string {
const args: string[] = [];
args.push(task);
@ -252,6 +208,21 @@ export async function createTask(
);
}
let fileName: string = path.basename(gradleBuildFileUri.fsPath);
if (taskName.includes(':')) {
const [subProjectName] = taskName.split(':');
const subProjectPath = path.join(folder.uri.fsPath, subProjectName);
const folderUri = vscode.Uri.file(subProjectPath);
const uri = (await getBuildFilePaths(folderUri))[0];
fileName = path.basename(uri.fsPath);
}
const definition: GradleTaskDefinition = {
type: 'gradle',
script: taskName,
fileName,
description: taskDescription
};
const relativeBuildGradle = getRelativePath(folder, gradleBuildFileUri);
if (relativeBuildGradle.length) {
definition.path = relativeBuildGradle;
@ -322,8 +293,6 @@ async function exists(file: string): Promise<boolean> {
});
}
type StringMap = { [s: string]: string };
const TASK_REGEX: RegExp = /$\s*(([^:\s]*:)?([a-z]+[A-Z0-9]?[a-z0-9]*[A-Za-z0-9]*))\b(\s-\s(.*))?/gm;
export function parseGradleTasks(buffer: Buffer | string): StringMap {