Skip to content

Feature/tutorial setup #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
further editor refactoring
  • Loading branch information
ShMcK committed Jun 9, 2019
commit 6d8b993d332134b8ab6b10ec364cc9ab86bf026d
75 changes: 42 additions & 33 deletions src/editor/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
// import * as vscode from 'vscode'
// import start from './start'
// // import ReactPanel from '../views/createWebview'
import * as vscode from 'vscode'
import { join } from 'path'
import { setStorage } from '../storage'
import ReactWebView from '../ReactWebView'
import * as CR from '../../typings'

// import runTest from './runTest'
// // import loadSolution from './loadSolution'
// // import quit from './quit'
const COMMANDS = {
START: 'coderoad.start',
OPEN_WEBVIEW: 'coderoad.open_webview',
OPEN_FILE: 'coderoad.open_file',
RUN_TEST: 'coderoad.test_run',
}

// const COMMANDS = {
// // TUTORIAL_SETUP: 'coderoad.tutorial_setup',
// START: 'coderoad.start',
// OPEN_WEBVIEW: 'coderoad.open_webview',
// RUN_TEST: 'coderoad.test_run',
// // LOAD_SOLUTION: 'coderoad.solution_load',
// // QUIT: 'coderoad.quit',
// }
interface CreateCommandProps {
context: vscode.ExtensionContext,
machine: CR.StateMachine
}

// React panel webview
let webview: any;

// export default (context: vscode.ExtensionContext): void => {
// const commands = {
// [COMMANDS.START]: () => {
// start(context)
// },
// [COMMANDS.OPEN_WEBVIEW]: () => {
// // ReactPanel.createOrShow(context.extensionPath);
// },
// [COMMANDS.RUN_TEST]: () => {
// runTest()
// },
// // [COMMANDS.LOAD_SOLUTION]: loadSolution,
// // [COMMANDS.QUIT]: () => quit(context.subscriptions),
// }
export const createCommands = ({ context, machine }: CreateCommandProps) => ({
[COMMANDS.START]: () => {
// set local storage workspace
setStorage(context.workspaceState)

// for (const cmd in commands) {
// const command: vscode.Disposable = vscode.commands.registerCommand(cmd, commands[cmd])
// context.subscriptions.push(command)
// }
// }
// activate machine
webview = new ReactWebView(context.extensionPath, machine.onReceive)
machine.activate()
},
[COMMANDS.OPEN_WEBVIEW]: () => {
webview.createOrShow();
},
[COMMANDS.OPEN_FILE]: async (relativeFilePath: string) => {
try {
const workspaceRoot = vscode.workspace.rootPath
if (!workspaceRoot) {
throw new Error('No workspace root path')
}
const absoluteFilePath = join(workspaceRoot, relativeFilePath)
const doc = await vscode.workspace.openTextDocument(absoluteFilePath)
await vscode.window.showTextDocument(doc, vscode.ViewColumn.One)
} catch (error) {
console.log(`Failed to open file ${relativeFilePath}`, error)
}
}
})
5 changes: 0 additions & 5 deletions src/editor/commands/quit.ts

This file was deleted.

53 changes: 15 additions & 38 deletions src/editor/index.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,35 @@
import * as vscode from 'vscode'
import * as CR from '../typings'
import { setStorage } from './storage'
import ReactWebView from './ReactWebView'
import { createCommands } from './commands'

interface Props {
machine: CR.StateMachine,
setWorkspaceRoot(rootPath: string): void
}

class Editor {
// extension context set on activation
// @ts-ignore
private context: vscode.ExtensionContext
private workspaceRoot: string | undefined
private machine: CR.StateMachine
private webview: any

private COMMANDS = {
START: 'coderoad.start',
OPEN_WEBVIEW: 'coderoad.open_webview',
RUN_TEST: 'coderoad.test_run',
}

constructor(machine: CR.StateMachine) {
constructor({ machine, setWorkspaceRoot }: Props) {
this.machine = machine
}

private commandStart = (): void => {
// set workspace root
const { rootPath } = vscode.workspace
// set workspace root for node executions
const { workspace } = vscode
const { rootPath } = workspace
if (!rootPath) {
throw new Error('Requires a workspace. Please open a folder')
}
this.workspaceRoot = rootPath

// set local storage workspace
setStorage(this.context.workspaceState)

// activate machine
this.webview = new ReactWebView(this.context.extensionPath, this.machine.onReceive)
this.machine.activate()

console.log('command start webview')
console.log(this.webview)
setWorkspaceRoot(rootPath)
}

private activateCommands = (): void => {
console.log('this.COMMANDS', this.COMMANDS)
const commands = {
[this.COMMANDS.START]: () => {
console.log('start')
this.commandStart()
},
[this.COMMANDS.OPEN_WEBVIEW]: () => {
console.log('open webview')
console.log(this.webview)
this.webview.createOrShow();
},
}
const commands = createCommands({
context: this.context,
machine: this.machine,
})
for (const cmd in commands) {
const command: vscode.Disposable = vscode.commands.registerCommand(cmd, commands[cmd])
this.context.subscriptions.push(command)
Expand Down
14 changes: 9 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { setWorkspaceRoot } from './services/node'
import StateMachine from './state'
import Editor from './editor'

// state machine that governs application logic
const Machine = new StateMachine()
// vscode editor
const VSCodeEditor = new Editor(Machine)
export const machine = new StateMachine()

export const activate = VSCodeEditor.activate
export const deactivate = VSCodeEditor.deactivate
// vscode editor
export const editor = new Editor({
machine,
setWorkspaceRoot,
})

export const activate = editor.activate
export const deactivate = editor.deactivate
5 changes: 3 additions & 2 deletions src/services/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode'
import * as CR from 'typings'
import { exec, exists, openFile } from '../node'
import { exec, exists } from '../node'

const gitOrigin = 'coderoad'

Expand Down Expand Up @@ -40,7 +41,7 @@ export async function gitLoadCommits(actions: CR.TutorialAction): Promise<void>

if (files) {
for (const filePath of files) {
openFile(filePath)
vscode.commands.executeCommand('coderoad.open_webview', filePath)
}
}
}
Expand Down
19 changes: 1 addition & 18 deletions src/services/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { workspace } from 'vscode'
import * as fs from 'fs'
import * as vscode from 'vscode'
import { join } from 'path'
import { exec as cpExec } from 'child_process'
import { promisify } from 'util'
Expand All @@ -11,11 +9,7 @@ let workspaceRoot: string

// set workspace root
// other function will use this to target the correct cwd
export async function setWorkspaceRoot(): Promise<void> {
const { rootPath } = workspace
if (!rootPath) {
throw new Error('Requires a workspace. Please open a folder')
}
export function setWorkspaceRoot(rootPath: string): void {
workspaceRoot = rootPath
}

Expand All @@ -28,17 +22,6 @@ export const exec = (cmd: string): Promise<{ stdout: string; stderr: string }> =
// collect all paths together
export const exists = (...paths: string[]): boolean => fs.existsSync(join(workspaceRoot, ...paths))

export const openFile = async (relativeFilePath: string): Promise<void> => {
try {
const absoluteFilePath = join(workspaceRoot, relativeFilePath)
const doc = await vscode.workspace.openTextDocument(absoluteFilePath)
await vscode.window.showTextDocument(doc, vscode.ViewColumn.One)
} catch (error) {
console.log(`Failed to open file ${relativeFilePath}`, error)
}
}


// export async function clear(): Promise<void> {
// // remove all files including ignored
// // NOTE: Linux only
Expand Down
1 change: 0 additions & 1 deletion src/services/testResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as CR from 'typings'
import * as vscode from 'vscode'
import * as storage from './storage'


export async function onSuccess(position: CR.Position) {
console.log('onSuccess', position)
vscode.window.showInformationMessage('SUCCESS')
Expand Down
3 changes: 1 addition & 2 deletions src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class StateMachine {
this.service = interpret(machine, this.machineOptions)
// logging
.onTransition(state => {
console.log('state', state)
if (state.changed) {
console.log('transition')
console.log('next state')
console.log(state.value)
}
})
Expand Down