Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
kube committed Sep 20, 2016
0 parents commit f77f901
Show file tree
Hide file tree
Showing 20 changed files with 1,243 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*]
indent_style = space
indent_size = 2
insert_final_newline = true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
out
node_modules
*.vsix
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// A launch configuration that compiles the extension and then opens it inside a new window
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "${workspaceRoot}/out/src",
"preLaunchTask": "npm"
},
{
"name": "Launch Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "${workspaceRoot}/out/test",
"preLaunchTask": "npm"
}
]
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
},
"typescript.tsdk": "./node_modules/typescript/lib" // we want to use the TS server from our node_modules folder to control its version
}
30 changes: 30 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process

// A task runner that calls a custom npm script that compiles the extension.
{
"version": "0.1.0",

// we want to run npm
"command": "npm",

// the command is a shell script
"isShellCommand": true,

// show the output window only if unrecognized errors occur.
"showOutput": "silent",

// we run the custom script "compile" as defined in package.json
"args": ["run", "compile", "--loglevel", "silent"],

// The tsc compiler is started in watching mode
"isWatching": true,

// use the standard tsc in watch mode problem matcher to find compile problems in the output.
"problemMatcher": "$tsc-watch"
}
9 changes: 9 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.vscode/**
typings/**
out/test/**
test/**
src/**
**/*.map
.gitignore
tsconfig.json
vsc-extension-quickstart.md
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Christophe Feijoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 42 header for VSCode
17 changes: 17 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#########.
########",#:
#########',##".
##'##'## .##',##.
## ## ## # ##",#.
## ## ## ## ##'
## ## ## :##
## ## ##."

# A little install script to
# remember howto install extension
npm install
vsce package
code --uninstall-extension kube.42header
code --install-extension 42header*.vsix
rm 42header*.vsix
55 changes: 55 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "42header",
"displayName": "42 Header",
"description": "42 header for VSCode : www.42.fr",
"version": "0.1.3",
"publisher": "kube",
"repository": {
"type": "git",
"url": "https://github.com/kube/vscode-42header"
},
"bugs": {
"url": "https://github.com/kube/vscode-42header/issues"
},
"homepage": "https://github.com/kube/vscode-42header",
"license": "MIT",
"engines": {
"vscode": "^1.4.0"
},
"categories": [
"Other"
],
"activationEvents": [
"*"
],
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "42header.insertHeader",
"title": "Insert 42 header",
"when": "editorTextFocus"
}
],
"keybindings": [
{
"command": "42header.insertHeader",
"key": "ctrl+alt+h",
"mac": "cmd+alt+h",
"when": "editorTextFocus"
}
]
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install"
},
"devDependencies": {
"typescript": "^2.0.0",
"vscode": "^0.11.0"
},
"dependencies": {
"moment": "^2.15.0"
}
}
56 changes: 56 additions & 0 deletions src/delimiters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

/*#######.
########",#:
#########',##".
##'##'## .##',##.
## ## ## # ##",#.
## ## ## ## ##'
## ## ## :##
## ## ##*/

const hashes = ['# ', ' #']
const slashes = ['/* ', ' */']
const semicolons = [';; ', ' ;;']
const parens = ['(* ', ' *)']
const dashes = ['-- ', ' --']
const percents = ['%% ', ' %%']

export const languageDemiliters: { [index: string]: string[] | null } = {
'c': slashes,
'coffeescript': hashes,
'cpp': slashes,
'css': slashes,
'dockerfile': hashes,
'fsharp': parens,
'go': slashes,
'groovy': slashes,
'haskell': dashes,
'ini': semicolons,
'jade': slashes,
'java': slashes,
'javascript': slashes,
'javascriptreact': slashes,
'latex': percents,
'less': slashes,
'lua': semicolons,
'makefile': hashes,
'objective-c': slashes,
'ocaml': parens,
'perl': hashes,
'perl6': hashes,
'php': slashes,
'plaintext': hashes,
'powershell': hashes,
'python': hashes,
'r': hashes,
'ruby': hashes,
'rust': slashes,
'scss': slashes,
'shellscript': hashes,
'sql': hashes,
'swift': slashes,
'typescript': slashes,
'typescriptreact': slashes,
'xsl': slashes,
'yaml': hashes
}
115 changes: 115 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

/*#######.
########",#:
#########',##".
##'##'## .##',##.
## ## ## # ##",#.
## ## ## ## ##'
## ## ## :##
## ## ##*/

'use strict'
import * as path from 'path'
import * as vscode from 'vscode'
import * as moment from 'moment'

import {
ExtensionContext, TextEdit, TextEditorEdit, TextDocument, Position, Range
} from 'vscode'

import {
extractHeader, getHeaderInfo, updateHeaderInfo, renderHeader,
supportsLanguage, IHeaderInfo
} from './header'

/**
* `insertHeader` Command Handler
*/
function insertHeaderHandler() {
let activeTextEditor = vscode.window.activeTextEditor
let document = activeTextEditor.document
let languageId = document.languageId

if (supportsLanguage(languageId)) {
activeTextEditor.edit(editor => {
let currentHeader = extractHeader(document.getText())

if (currentHeader) {
let headerInfo = getHeaderInfo(currentHeader)
let updatedHeaderInfo = updateHeaderInfo(headerInfo)
let header = renderHeader(languageId, updatedHeaderInfo)

editor.replace(new Range(0, 0, 12, 0), header)
}
else {
let user = process.env['USER']
let headerInfo = {
filename: path.basename(document.fileName),
author: `${user} <${user}@student.42.fr>`,
createdBy: user,
createdAt: moment(),
updatedBy: user,
updatedAt: moment()
}
let header = renderHeader(languageId, headerInfo)
editor.insert(new Position(0, 0), header)
}
})
}
else
vscode.window.showInformationMessage(
`No header support for language ${languageId}`)
}

/**
* Start watcher for document save to update current header
* if broken by code-formatter
*/
function startHeaderUpdateOnSaveWatcher(subscriptions: vscode.Disposable[]) {
const ignoreNextSave = new WeakSet()

// Here we use the trick from Luke Hoban Go Extension,
// But this will cause an infinite loop if another extension that
// uses the same technique is active.
// And it's really ugly.
vscode.workspace.onDidSaveTextDocument(document => {
let textEditor = vscode.window.activeTextEditor

if (textEditor.document === document
&& !ignoreNextSave.has(document)) {
let activeTextEditor = vscode.window.activeTextEditor
let languageId = document.languageId
let currentHeader = extractHeader(document.getText())

// If current language is supported
// and a header is present at top of document
if (supportsLanguage(languageId) && currentHeader) {
textEditor.edit(editor => {
let headerInfo = getHeaderInfo(currentHeader)
let updatedHeaderInfo = updateHeaderInfo(headerInfo)
let header = renderHeader(languageId, updatedHeaderInfo)

editor.replace(new Range(0, 0, 12, 0), header)
})
.then(applied => {
ignoreNextSave.add(document)
return document.save()
})
.then(() => {
ignoreNextSave.delete(document)
}, err => { })
}
}
}, null, subscriptions)
}

export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands
.registerTextEditorCommand('42header.insertHeader', insertHeaderHandler)

context.subscriptions.push(disposable)
startHeaderUpdateOnSaveWatcher(context.subscriptions)
}

export function deactivate() {
}
Loading

0 comments on commit f77f901

Please sign in to comment.