Skip to content

Commit

Permalink
Add git support
Browse files Browse the repository at this point in the history
  • Loading branch information
marlomgirardi committed Oct 5, 2019
1 parent 758f3c2 commit b1d78c1
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 13 deletions.
21 changes: 18 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension (Empty)",
"name": "Run (Empty)",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
Expand All @@ -17,7 +17,7 @@
"preLaunchTask": "npm: watch"
},
{
"name": "Run Extension (1 Folder)",
"name": "Run (1 Folder)",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
Expand All @@ -31,7 +31,22 @@
"preLaunchTask": "npm: watch"
},
{
"name": "Extension Tests",
"name": "Run without extensions (1 Folder)",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"${workspaceFolder}/src/test/projects/test-1"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"preLaunchTask": "npm: watch"
},
{
"name": "Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.1.0 - 2019-10-05

### Added

- Git support

## 0.0.1 - 2019-10-03

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ With this extension you won't forget the files that you need to open to continue

### Task management

In a simple way you can create tasks an track the opened files without get lost when switching context.
In a simple way you can:
- Create/edit/delete task
- Track opened files by task
- Track git branch by task

![Task management](images/docs/task-management.gif)

## Limitations

- It may work in workspaces with more than one folder, but it only supports single folders.

## Known Issues

We are just starting but we know that we will have issues, to avoid duplicated issues they will be here :beetle:
Expand Down
Binary file modified images/docs/task-management.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "keep-context",
"displayName": "Keep Context",
"description": "Make context switch easy to be managed and tracked.",
"version": "0.0.1",
"version": "0.1.0",
"publisher": "marlom",
"icon": "images/keep-context.png",
"engines": {
Expand Down Expand Up @@ -90,6 +90,14 @@
"pretest": "npm run compile",
"test": "node ./out/test/runTest.js"
},
"keywords": [
"context",
"switch",
"task"
],
"extensionDependencies": [
"vscode.git"
],
"repository": {
"type": "git",
"url": "git+https://github.com/marlomgirardi/vscode-keep-context.git"
Expand All @@ -100,12 +108,12 @@
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.9",
"@types/node": "^12.7.11",
"@types/vscode": "^1.38.0",
"glob": "^7.1.4",
"mocha": "^6.2.1",
"typescript": "^3.6.3",
"tslint": "^5.20.0",
"typescript": "^3.6.3",
"vscode-test": "^1.2.0"
}
}
71 changes: 71 additions & 0 deletions src/GitProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Extension, extensions } from 'vscode';
import * as Git from './git';

export default class GitProvider {
branch?: string;

/**
* Listen to branch changes
*/
onDidChangeBranch?: (branch?: string) => void;

/**
* Listen to git initialization
*/
onDidInitialize?: (branch?: string) => void;

/**
* Access to the native git extension
*/
private git: Git.API;

constructor() {
const gitExtension: Extension<Git.GitExtension> | undefined = extensions.getExtension('vscode.git');

if (!gitExtension) {
throw new Error('Could not found the vscode.git extension');
}

this.git = gitExtension.exports.getAPI(1);

if (this.git.state === 'initialized') {
this.branch = this.getBranch();
this.listenToBranchChange();
if (this.onDidInitialize) {
this.onDidInitialize(this.branch);
}
} else {
this.git.onDidChangeState((state) => {
if (state === 'initialized') {
this.branch = this.getBranch();
this.listenToBranchChange();
if (this.onDidInitialize) {
this.onDidInitialize(this.branch);
}
}
});
}
}

setBranch(branch: string): void {
this.git.repositories[0].checkout(branch);
}

private listenToBranchChange() {
this.git.repositories[0].state.onDidChange(() => {
const newBranch = this.getBranch();
if (newBranch !== this.branch) {
this.branch = newBranch;
if (this.onDidChangeBranch) {
this.onDidChangeBranch(this.branch);
}
}
});
}

private getBranch(): string | undefined {
if (this.git && this.git.repositories[0] && this.git.repositories[0].state.HEAD) {
return this.git.repositories[0].state.HEAD.name;
}
}
}
51 changes: 49 additions & 2 deletions src/KeepContext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as path from 'path';
import { commands, StatusBarAlignment, StatusBarItem, TextDocument, Uri, ViewColumn, window, workspace } from 'vscode';
import {
commands, StatusBarAlignment, StatusBarItem, TextDocument, Uri, ViewColumn, window, workspace,
} from 'vscode';

import KeepContext from '.';
import { ContextTreeDataProvider } from './ContextTreeDataProvider';
import { ContextTreeItem } from './ContextTreeItem';
import GitProvider from './GitProvider';
import Settings from './Settings';
import { createTask, getRealFileName, taskInputBox } from './utils';

Expand Down Expand Up @@ -37,19 +40,29 @@ export default class KeepContext {
private vsCodeSettings?: string;

/**
* Keep Context settings management
* Keep Context settings management.
*/
private settings: Settings;

/**
* Git provider.
*/
private git: GitProvider;

constructor() {
if (!workspace.workspaceFolders) {
throw new Error('A workspace is required to run Keep Context.');
}

this.vsCodeSettings = path.join(workspace.workspaceFolders[0].uri.fsPath, '.vscode');

this.git = new GitProvider();

this.settings = new Settings(this.vsCodeSettings);

this.git.onDidChangeBranch = this.onBranchChange;
this.git.onDidInitialize = this.onGitInitialize;

if (this.settings.activeTask) {
const task = this.settings.tasks[this.settings.activeTask];
this.updateStatusBar(task.name);
Expand All @@ -70,6 +83,8 @@ export default class KeepContext {

const task = createTask(taskName);

task.branch = this.git.branch;

// TODO: Add opened files to the current task?

if (this.settings.tasks[task.id]) {
Expand Down Expand Up @@ -167,6 +182,10 @@ export default class KeepContext {

const task = this.settings.tasks[taskId];

if (task.branch) {
this.git.setBranch(task.branch);
}

this.updateStatusBar(task.name);

task.files
Expand Down Expand Up @@ -231,4 +250,32 @@ export default class KeepContext {
this.statusBarItem.hide();
}
}

/**
* Handles the branch change.
* @param branch The new branch
*/
private onBranchChange = (branch?: string) => {
if (this.settings.activeTask) {
const task = this.settings.tasks[this.settings.activeTask];

task.branch = branch;
this.settings.save();
}
}

/**
* Handles the Git Initialization
*/
private onGitInitialize = () => {
if (this.settings.activeTask) {
const task = this.settings.tasks[this.settings.activeTask];
if (task.branch) {
this.git.setBranch(task.branch);
} else {
task.branch = this.git.branch;
this.settings.save();
}
}
}
}
Loading

0 comments on commit b1d78c1

Please sign in to comment.