This repository has been archived by the owner on Sep 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
Template creation feature and file creation changes #3
Open
Yantrio
wants to merge
5
commits into
jchannon:master
Choose a base branch
from
Yantrio:quickpick
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import {TemplateQuickPickItem} from './TemplateQuickPickItem'; | ||
import { execFile } from 'child_process'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as vscode from 'vscode'; | ||
|
||
export class TemplateManager { | ||
|
||
templateDirectory = vscode.extensions.getExtension('jchannon.csharpextensions').extensionPath + '/templates/'; | ||
|
||
installMessage = 'Install'; | ||
|
||
createTemplate() { | ||
vscode.window.showInputBox({ ignoreFocusOut: true, prompt: 'Please enter template name', value: "templateName.tmpl" }).then(result => { | ||
let newTemplateFileName = path.join(this.templateDirectory, result); | ||
this.createNewTemplateFile(newTemplateFileName); | ||
this.openTemplate(newTemplateFileName); | ||
}); | ||
} | ||
|
||
editTemplate() { | ||
this.showTemplateQuickPick() | ||
.then(selectedTemplate => { | ||
var templateFileName = path.join(this.templateDirectory, selectedTemplate.template + '.tmpl'); | ||
this.openTemplate(templateFileName); | ||
}); | ||
} | ||
|
||
openTemplate(template: string) { | ||
execFile('code', [this.templateDirectory, template], (error: any, stdout, stderr) => { | ||
if (error.code === 'ENOENT') { | ||
vscode.window.showErrorMessage('Please install \'code\' command into the PATH', this.installMessage).then(value => { | ||
if (value === this.installMessage) { | ||
vscode.commands.executeCommand('workbench.action.installCommandLine'); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
showTemplateQuickPick(): Thenable<TemplateQuickPickItem> { | ||
let templates = this.getTemplateQuickPickItems(); | ||
return vscode.window.showQuickPick(Promise.all<TemplateQuickPickItem>(templates) as Thenable<Iterable<TemplateQuickPickItem>>); | ||
} | ||
|
||
getTemplateQuickPickItems(): PromiseLike<TemplateQuickPickItem>[] { | ||
let templateDirectory = vscode.extensions.getExtension('jchannon.csharpextensions').extensionPath + '/templates/'; | ||
return fs.readdirSync(templateDirectory).map(file => { | ||
let templateName = path.basename(file, '.tmpl'); | ||
return vscode.workspace.openTextDocument(path.join(templateDirectory, file)).then(doc => { | ||
let text = doc.getText(); | ||
let regex = /\$\{Description:(.*)\}/i; | ||
let description = text.match(regex)[1]; | ||
return new TemplateQuickPickItem(templateName, description, "", templateName); | ||
}); | ||
}); | ||
} | ||
|
||
createNewTemplateFile(newTemplateFile: string) { | ||
if (!fs.exists(newTemplateFile)) { | ||
fs.writeFileSync(newTemplateFile, `\${Description: Template Description Here} | ||
//use \${namespace} to set namespace | ||
//use \${name} to set name | ||
//use \${cursor} to set where the cursor will go when the file is first opened | ||
`); | ||
} | ||
} | ||
|
||
findCursorInTemlpate(text: string) { | ||
let cursorPos = text.indexOf('${cursor}'); | ||
let preCursor = text.substr(0, cursorPos); | ||
let lineNum = preCursor.match(/\n/gi).length; | ||
let charNum = preCursor.substr(preCursor.lastIndexOf('\n')).length; | ||
return new vscode.Position(lineNum, charNum); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
export class TemplateQuickPickItem implements vscode.QuickPickItem | ||
{ | ||
constructor(public label: string, | ||
public description: string, | ||
public detail: string, | ||
public template: string ){ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { extname } from 'path'; | ||
|
||
export function ensureExtension(filename: string, extension: string) { | ||
if (extname(filename) !== extension) { | ||
if (filename.endsWith('.')) { | ||
filename = filename + extension.replace('.',''); | ||
} else { | ||
filename = filename + extension; | ||
} | ||
} | ||
return filename; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
${Description: Template Description Here} | ||
//use ${namespace} to set namespace | ||
//use ${name} to set name | ||
//use ${cursor} to set where the cursor will go when the file is first opened | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is tslint something that needs to go in package.json There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it should be bought in as a dev dependency, but for now, I was just installing it globally. |
||
"rules": { | ||
"no-unused-expression": true, | ||
"no-duplicate-variable": true, | ||
"no-duplicate-key": true, | ||
"no-unused-variable": true, | ||
"curly": true, | ||
"class-name": true, | ||
"semicolon": ["always"], | ||
"triple-equals": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Denied
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you have against classes? or is it that I called it 'Manager' ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the latter 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming is hard, what do you suggest?