Skip to content
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

Adding support for different comment styles + multi-line notes #13

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function getConfiguration(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration('copyrighter');
}

export const configuredLanguages = new Set([
// comment styles '/* */' or '//'
export const cstyleLanguages = new Set([
'c',
'cpp',
'csharp',
Expand All @@ -43,6 +44,17 @@ export const configuredLanguages = new Set([
'vue'
]);

// comments use '#'
export const hashtagLanguages = new Set([
'python',
'shellscript'
]);

export const configuredLanguages = new Set([
...cstyleLanguages,
...hashtagLanguages
]);

export function getAuthor(): string {
return getConfiguration().get('author') || '';
}
Expand Down
21 changes: 21 additions & 0 deletions src/copyright/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2019 Ford Motor Company
* All rights reserved.

* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at

* http://www.apache.org/licenses/LICENSE-2.0

* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export enum CommentStyle {
CStyle,
Hashtag
}
34 changes: 28 additions & 6 deletions src/copyright/copyright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'use script';

import * as configuration from '../configuration';
import * as comment from './comment';

export class Copyright {
protected author: string;
Expand All @@ -30,16 +31,37 @@ export class Copyright {
this.note = configuration.getNote();
}

public header(): string {
let template = `/*
* Copyright (c) ${this.year} ${this.author}
* All rights reserved.\n`;
public header(style: comment.CommentStyle): string {
let beginningPrefix: String;
let midPrefix: String;
let endPrefix: String;

switch (style) {
case comment.CommentStyle.CStyle:
default:
beginningPrefix = '/*';
midPrefix = ' *'
endPrefix = ' */'
break;

case comment.CommentStyle.Hashtag:
beginningPrefix = '#';
midPrefix = '#'
endPrefix = '#'
break;
}

let template = `${beginningPrefix}
${midPrefix} Copyright (c) ${this.year} ${this.author}
${midPrefix} All rights reserved.\n`;

if (this.note) {
template += ` * ${this.note}\n`;
this.note.split('\n').forEach((line: String) => {
template += `${midPrefix} ${line}\n`;
});
}

template += ` */\n`;
template += `${endPrefix}\n`;

return template;
}
Expand Down
15 changes: 14 additions & 1 deletion src/copyright/copyrightService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import * as vscode from 'vscode';
import * as configuration from '../configuration';
import * as comment from './comment';

export function handleCopyrightCheck(editor: vscode.TextEditor | undefined) {
if (
Expand Down Expand Up @@ -55,7 +56,7 @@ function hasCopyright(document: vscode.TextDocument): Boolean {

function insertCopyright(editor: vscode.TextEditor) {
const documentStartPosition = new vscode.Position(0, 0);
const copyright = configuration.getCopyright().header();
const copyright = configuration.getCopyright().header(getCommentStyle(editor.document.languageId));

editor.edit(document => {
document.insert(documentStartPosition, copyright);
Expand All @@ -69,3 +70,15 @@ function isNewDocument(document: vscode.TextDocument): Boolean {
function isSupportedLanguage(languageId: string): Boolean {
return configuration.configuredLanguages.has(languageId);
}

function getCommentStyle(languageId: string): comment.CommentStyle {
if (configuration.cstyleLanguages.has(languageId)) {
return comment.CommentStyle.CStyle;
}
if (configuration.hashtagLanguages.has(languageId)) {
return comment.CommentStyle.Hashtag;
}

// default to C-style (this shouldn't be reached anyway!)
return comment.CommentStyle.CStyle;
}