Skip to content

Commit

Permalink
Add new command 'Chcek available languages'.
Browse files Browse the repository at this point in the history
Replace graceful-fs to vscode.workspace.fs.
  • Loading branch information
uctakeoff committed Feb 8, 2020
1 parent 650cc25 commit 58c08fd
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 41 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## [Unreleased]

* workspace counter in status bar.

## [1.3.3]

### Added

- New Command : `Check available languages`.

## [1.3.2]

### Fixed

- Replaced the file API used with one provided by vscode.

## [1.3.1]

Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ VS Code extension: counts blank lines, comment lines, and physical lines of sour

### Count in workspace

* Open the command palette and select `VSCode Counter: Count lines in workspace`.
* Open the command palette and select `VSCodeCounter: Count lines in workspace`.

![](images/count_workspace.gif)

Expand All @@ -30,11 +30,16 @@ VS Code extension: counts blank lines, comment lines, and physical lines of sour

### Real-time counter

* Open the command palette and select `VSCode Counter: Toggle Real-time Counter Visibility`.
* Open the command palette and select `VSCodeCounter: Toggle Real-time Counter Visibility`.

![](images/realtime_counter.png)

### Check available languages

* Open the command palette and select `VSCodeCounter: Check available languages`.
![](images/avail_langs.png)

* Available languages can be increased by adding language extensions.

## Extension Settings

Expand Down
Binary file added images/avail_langs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-counter",
"displayName": "VS Code Counter",
"description": "Count lines of code in many programming languages.",
"version": "1.3.2",
"version": "1.3.3",
"publisher": "uctakeoff",
"author": {
"name": "Ushiyama Kentaro"
Expand Down Expand Up @@ -36,7 +36,8 @@
"*",
"onCommand:extension.vscode-counter.countInFile",
"onCommand:extension.vscode-counter.countInDirectory",
"onCommand:extension.vscode-counter.countInWorkspace"
"onCommand:extension.vscode-counter.countInWorkspace",
"onCommand:extension.vscode-counter.outputAvailableLanguages"
],
"contributes": {
"commands": [
Expand All @@ -57,6 +58,12 @@
"category": "VSCodeCounter",
"title": "%commands.countInWorkspace.title%",
"icon": "images/icon.png"
},
{
"command": "extension.vscode-counter.outputAvailableLanguages",
"category": "VSCodeCounter",
"title": "%commands.outputAvailableLanguages.title%",
"icon": "images/icon.png"
}
],
"menus": {
Expand Down
1 change: 1 addition & 0 deletions package.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"commands.countInFile.title": "リアルタイムコードカウンタの表示・非表示切り替え",
"commands.countInDirectory.title": "ディレクトリ内のコード行を数える",
"commands.countInWorkspace.title": "ワークスペース内のコード行を数える",
"commands.outputAvailableLanguages.title": "利用可能な言語を確認する",
"configuration.showInStatusBar.description": "現在のファイルのカウント結果をリアルタイムでステータスバーに表示する.",
"configuration.useGitignore.description": "対象ファイルを除外するために '.gitignore' を使う.",
"configuration.useFilesExclude.description": "対象ファイルを除外するために 'files.exclude' 設定を使う.",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"commands.countInFile.title": "Toggle Real-time Counter Visibility",
"commands.countInDirectory.title": "Count lines in directory",
"commands.countInWorkspace.title": "Count lines in workspace",
"commands.outputAvailableLanguages.title": "Check available languages",
"configuration.showInStatusBar.description": "Whether to show the count result of the current file on the status bar in real time.",
"configuration.useGitignore.description": "Whether to use '.gitignore' files for excluding files.",
"configuration.useFilesExclude.description": "Whether to use setting 'files.exclude' for excluding files.",
Expand Down
4 changes: 1 addition & 3 deletions src/LineCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export default class LineCounter
this.id = id;
}
public addAlias(aliases: string[]) {
if (aliases) {
this.aliases.push(...aliases);
}
this.aliases.push(...aliases);
}
public addLineCommentRule(...tokens: string[]) {
this.lineComment.push(...tokens);
Expand Down
102 changes: 68 additions & 34 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'graceful-fs';
// import * as fs from 'graceful-fs';
import LineCounter from './LineCounter';
import Gitignore from './Gitignore';
import * as JSONC from 'jsonc-parser';
Expand Down Expand Up @@ -44,7 +44,8 @@ export function activate(context: vscode.ExtensionContext) {
codeCountController,
vscode.commands.registerCommand('extension.vscode-counter.countInWorkspace', () => codeCountController.countInWorkspace()),
vscode.commands.registerCommand('extension.vscode-counter.countInDirectory', (targetDir: vscode.Uri|undefined) => codeCountController.countInDirectory(targetDir)),
vscode.commands.registerCommand('extension.vscode-counter.countInFile', () => codeCountController.toggleVisible())
vscode.commands.registerCommand('extension.vscode-counter.countInFile', () => codeCountController.toggleVisible()),
vscode.commands.registerCommand('extension.vscode-counter.outputAvailableLanguages', () => codeCountController.outputAvailableLanguages())
);
}
// this method is called when your extension is deactivated
Expand Down Expand Up @@ -95,6 +96,9 @@ class CodeCounterController {
public toggleVisible() {
this.configuration.update('showInStatusBar', !this.isVisible);
}
public outputAvailableLanguages() {
this.codeCounter.outputAvailableLanguages();
}
public countInDirectory(targetDir: vscode.Uri|undefined) {
try {
const folders = workspaceFolders();
Expand Down Expand Up @@ -248,16 +252,19 @@ class CodeCounter {
private outputChannel: vscode.OutputChannel|null = null;
private statusBarItem: vscode.StatusBarItem|null = null;
private configuration: vscode.WorkspaceConfiguration;
private langExtensions: VscodeLangExtension[];
private lineCounterTable: LineCounterTable;

constructor(configuration: vscode.WorkspaceConfiguration) {
log(`create CodeCounter`);
log(`build CodeCounter start`);
this.configuration = configuration;
const confFiles = vscode.workspace.getConfiguration("files", null);
this.lineCounterTable = new LineCounterTable(this.configuration, [...Object.entries(confFiles.get<object>('associations', {}))]);
this.langExtensions = loadLanguageExtensions();
this.lineCounterTable = new LineCounterTable(this.langExtensions, this.configuration, [...Object.entries(confFiles.get<object>('associations', {}))]);
if (this.getConf('showInStatusBar', false)) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
}
log(`create CodeCounter end`);
}
dispose() {
if (this.statusBarItem !== null) {
Expand All @@ -278,6 +285,12 @@ class CodeCounter {
this.outputChannel.show();
this.outputChannel.appendLine(text);
}
public outputAvailableLanguages() {
this.langExtensions.forEach((lang) => {
this.toOutputChannel(`${lang.id} : aliases[${lang.aliases}], extensions[${lang.extensions}], filenames:[${lang.filenames}]`);
});
this.toOutputChannel(`VS Code Counter : available all ${this.langExtensions.length} languages.`);
}
public countLinesInDirectory(targetUri: vscode.Uri, outputDirUri: vscode.Uri) {
// const outputDir = path.resolve(outputDirUri.fsPath, this.getConf('outputDirectory', '.VSCodeCounter'));
const outputDir = buildUri(outputDirUri, this.getConf('outputDirectory', '.VSCodeCounter'));
Expand Down Expand Up @@ -407,14 +420,45 @@ class CodeCounter {
}
}

class VscodeLangExtension {
extensionPath: string;
id:string;
aliases:string[];
filenames:string[];
extensions:string[];
configuration:vscode.Uri|undefined;

constructor(extensionPath:string, language:{id:string, aliases:string[]|undefined, filenames:string[]|undefined, extensions:string[]|undefined, configuration:string|undefined}) {
this.extensionPath = extensionPath;
this.id = language.id;
this.aliases = language.aliases !== undefined ? language.aliases : [];
this.filenames = language.filenames !== undefined ? language.filenames : [];
this.extensions = language.extensions !== undefined ? language.extensions : [];
this.configuration = language.configuration !== undefined ? vscode.Uri.file(path.join(this.extensionPath, language.configuration)) : undefined;
}
}
function loadLanguageExtensions() : VscodeLangExtension[] {
const ret : VscodeLangExtension[] = [];
vscode.extensions.all.forEach(ex => {
const contributes = ex.packageJSON.contributes;
if (contributes !== undefined) {
const languages = contributes.languages;
if (languages !== undefined) {
(languages as Array<any>).forEach(l => ret.push(new VscodeLangExtension(ex.extensionPath, l)));
}
}
});
return ret;
}

class LineCounterTable {
private langIdTable: Map<string, LineCounter>;
private aliasTable: Map<string, LineCounter>;
private fileextRules: Map<string, LineCounter>;
private filenameRules: Map<string, LineCounter>;
private associations: [string, string][];

constructor(conf: vscode.WorkspaceConfiguration, associations: [string, string][]) {
constructor(langExtensions: VscodeLangExtension[], conf: vscode.WorkspaceConfiguration, associations: [string, string][]) {
this.langIdTable = new Map<string, LineCounter>();
this.aliasTable = new Map<string, LineCounter>();
this.fileextRules = new Map<string, LineCounter>();
Expand All @@ -423,38 +467,28 @@ class LineCounterTable {
log(`associations : ${this.associations.length}\n[${this.associations.join("],[")}]`);

const confJsonTable = new Map<string, object>();
const decoderU8 = new TextDecoder('utf8');

vscode.extensions.all.forEach(ex => {
const contributes = ex.packageJSON.contributes;
if (contributes !== undefined) {
const languages = contributes.languages;
if (languages !== undefined) {
languages.forEach((lang: {id:string, aliases:string[]|undefined, filenames:string[]|undefined, extensions:string[]|undefined, configuration:string|undefined}) => {
const lineCounter = getOrSetFirst(this.langIdTable, lang.id, () => new LineCounter(lang.id));
if (lang.aliases !== undefined) {
lineCounter.addAlias(lang.aliases);
lang.aliases.forEach((alias:string) => {
this.aliasTable.set(alias, lineCounter);
});
}
// log(`${lang.id} : aliases[${lang.aliases}], extensions[${lang.extensions}], filenames:[${lang.filenames}]`);
const confpath = lang.configuration ? path.join(ex.extensionPath, lang.configuration) : "";
if (confpath.length > 0) {
// log(` language conf file: ${confpath}`);
const v = getOrSetFirst(confJsonTable, confpath, () => JSONC.parse(fs.readFileSync(confpath, "utf8")));
// log(` ${JSON.stringify(v)}`);
lineCounter.addCommentRule(v.comments);
}
if (lang.extensions !== undefined) {
(lang.extensions as Array<string>).forEach(ex => this.fileextRules.set(ex.startsWith('.') ? ex : `.${ex}`, lineCounter));
}
if (lang.filenames !== undefined) {
(lang.filenames as Array<string>).forEach(ex => this.filenameRules.set(ex, lineCounter));
}
});
}
langExtensions.forEach(lang => {
// log(`${lang.id} : aliases[${lang.aliases}], extensions[${lang.extensions}], filenames:[${lang.filenames}], configuration:[${lang.configuration}]`);
const lineCounter = getOrSetFirst(this.langIdTable, lang.id, () => new LineCounter(lang.id));
lineCounter.addAlias(lang.aliases);
lang.aliases.forEach((alias:string) => {
this.aliasTable.set(alias, lineCounter);
});
const confpath = lang.configuration;
if (confpath !== undefined) {
vscode.workspace.fs.readFile(confpath).then(data => {
// log(`"${confpath}" : ${data.length}B`);
const v = getOrSetFirst(confJsonTable, confpath.toString(), () => JSONC.parse(decoderU8.decode(data)));
// log(` ${JSON.stringify(v)}`);
lineCounter.addCommentRule(v.comments);
});
}
lang.extensions.forEach(ex => this.fileextRules.set(ex.startsWith('.') ? ex : `.${ex}`, lineCounter));
lang.filenames.forEach(ex => this.filenameRules.set(ex, lineCounter));
});

class BlockPattern {
public types: string[] = [];
public patterns: string[][] = [];
Expand Down

0 comments on commit 58c08fd

Please sign in to comment.