Skip to content

Commit

Permalink
Add Function: Count the range of the selected text
Browse files Browse the repository at this point in the history
  • Loading branch information
uctakeoff committed Aug 10, 2020
1 parent a773f6a commit 7367271
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 56 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

* workspace counter in status bar.

## [2.1.0]

### Added

- New Function : Count the range of the selected text.

## [2.0.0]

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

## VSCode Counter 2.0 is released!

* Experimental support for "**VSCode Remove Development**".
* Experimental support for "**VSCode Remote Development**".

When using VSCode Counter in *VSCode Remove Development*, use the `Save language configurations` function You will need to.
When using VSCode Counter in *VSCode Remote Development*, use the `Save language configurations` function You will need to.
[See below for more information](#save-language-configurations).

## Features
Expand Down Expand Up @@ -41,6 +41,10 @@ VS Code extension: counts blank lines, comment lines, and physical lines of sour

![](images/realtime_counter.png)

* Count the range of the selected text

![](images/realtime_counter-select.gif)

### Check available languages

* Open the command palette and select `VSCodeCounter: Check available languages`.
Expand All @@ -52,9 +56,9 @@ VS Code extension: counts blank lines, comment lines, and physical lines of sour

**VSCode Counter** is able to aggregate in unknown languages by referring to the information in the installed language extensions. However, I found out that **this information is not available in Remote Development**.

Therefore, I modified VSCode Counter's ability to **collecting VSCode language extensions** so that it can be called as an independent function. The idea is to collect the information once in the local environment and store it so that it can be used remotely.
Therefore, VSCode Counter's ability of **collecting VSCode language extensions** is now called as an independent function. The idea is to collect the information once in the local environment and store it so that it can be used remotely.

* First, launch VSCode on your local.
* First, launch VSCode on your local server.
* Then, open the command palette and select `VSCodeCounter: Save the collected language configurations`.
* Then *settings.json* will store the configuration information collected from the current language extensions .
![](images/save_lang.png)
Expand All @@ -81,4 +85,4 @@ However, you must carry the stored information to the remote environment by your
* `VSCodeCounter.outputPreviewType`: Type of output file to preview after counting. Choose from `text`, `csv`, `markdown` or `none`.
* `VSCodeCounter.saveLocation`: Specify where to store the collected language configurations.

**Enjoy!**
**Enjoy!**
Binary file added images/realtime_counter-select.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion 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": "2.0.0",
"version": "2.1.0",
"publisher": "uctakeoff",
"author": {
"name": "Ushiyama Kentaro"
Expand Down
131 changes: 81 additions & 50 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function activate(context: vscode.ExtensionContext) {
const codeCountController = new CodeCounterController();
context.subscriptions.push(
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.countInWorkspace', () => codeCountController.countLinesInWorkSpace()),
vscode.commands.registerCommand('extension.vscode-counter.countInDirectory', (targetDir: vscode.Uri|undefined) => codeCountController.countLinesInDirectory(targetDir)),
vscode.commands.registerCommand('extension.vscode-counter.countInFile', () => codeCountController.toggleVisible()),
vscode.commands.registerCommand('extension.vscode-counter.saveLanguageConfigurations', () => codeCountController.saveLanguageConfigurations()),
vscode.commands.registerCommand('extension.vscode-counter.outputAvailableLanguages', () => codeCountController.outputAvailableLanguages())
Expand Down Expand Up @@ -66,9 +66,10 @@ class CodeCounterController {
// subscribe to selection change and editor activation events
let subscriptions: vscode.Disposable[] = [];
vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, subscriptions);
vscode.window.onDidChangeTextEditorSelection(this.onDidChangeTextEditorSelection, this, subscriptions);
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, subscriptions);
vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, subscriptions);
vscode.workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders, this, subscriptions);
// vscode.workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders, this, subscriptions);
// create a combined disposable from both event subscriptions
this.disposable = vscode.Disposable.from(...subscriptions);
}
Expand All @@ -80,6 +81,45 @@ class CodeCounterController {
this.disposable.dispose();
this.codeCounter_ = null;
}
// private onDidChangeWorkspaceFolders(e: vscode.WorkspaceFoldersChangeEvent) {
// log(`onDidChangeWorkspaceFolders()`);
// // e.added.forEach((f) => log(` added [${f.index}] ${f.name} : ${f.uri}`));
// // e.removed.forEach((f) => log(` removed [${f.index}] ${f.name} : ${f.uri}`));
// // vscode.workspace.workspaceFolders?.forEach((f) => log(` [${f.index}] ${f.name} : ${f.uri}`));
// }
private onDidChangeActiveTextEditor(e: vscode.TextEditor|undefined) {
if (this.codeCounter_) {
// log(`onDidChangeActiveTextEditor(${!e ? 'undefined' : e.document.uri})`);
this.countLinesInEditor(e);
}
}
private onDidChangeTextEditorSelection(e: vscode.TextEditorSelectionChangeEvent) {
if (this.codeCounter_) {
// log(`onDidChangeTextEditorSelection(${e.selections.length}selections, ${e.selections[0].isEmpty} )`, e.selections[0]);
this.countLinesInEditor(e.textEditor);
}
}
private onDidChangeTextDocument(e: vscode.TextDocumentChangeEvent) {
if (this.codeCounter_) {
// log(`onDidChangeTextDocument(${e.document.uri})`);
this.countLinesOfFile(e.document);
}
}
private onDidChangeConfiguration() {
// log(`onDidChangeConfiguration()`);
this.codeCounter_ = null;
this.countLinesInEditor(vscode.window.activeTextEditor);
}
public toggleVisible() {
if (!this.statusBarItem) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
this.countLinesInEditor(vscode.window.activeTextEditor);
} else {
this.statusBarItem.dispose();
this.statusBarItem = null;
}
}

private async getCodeCounter() {
if (this.codeCounter_) {
return this.codeCounter_
Expand Down Expand Up @@ -109,13 +149,13 @@ class CodeCounterController {
.catch(reason => showError(`outputAvailableLanguages() failed.`, reason));
}

public async countInDirectory(targetDir: vscode.Uri|undefined) {
public async countLinesInDirectory(targetDir: vscode.Uri|undefined) {
try {
const folder = await currentWorkspaceFolder();
if (!folder) {
showError(`No open workspace`);
} else if (targetDir) {
this.countLinesInDirectory(targetDir, folder.uri);
this.countLinesInDirectory_(targetDir, folder.uri);
} else {
const option = {
value : folder.uri.toString(true),
Expand All @@ -124,74 +164,60 @@ class CodeCounterController {
};
const uri = await vscode.window.showInputBox(option);
if (uri) {
this.countLinesInDirectory(vscode.Uri.parse(uri), folder.uri);
this.countLinesInDirectory_(vscode.Uri.parse(uri), folder.uri);
}
}
} catch (e) {
showError(`countInDirectory() failed.`, e.message);
showError(`countLinesInDirectory() failed.`, e.message);
}
}
public async countInWorkspace() {
public async countLinesInWorkSpace() {
try {
const folder = await currentWorkspaceFolder();
if (folder) {
this.countLinesInDirectory(folder.uri, folder.uri);
this.countLinesInDirectory_(folder.uri, folder.uri);
} else {
showError(`No folder are open.`);
}
} catch (e) {
showError(`countInWorkspace() failed.`, e.message);
showError(`countLinesInWorkSpace() failed.`, e.message);
}
}
private countLinesInDirectory(targetUri: vscode.Uri, workspaceDir: vscode.Uri) {
private countLinesInDirectory_(targetUri: vscode.Uri, workspaceDir: vscode.Uri) {
const conf = vscode.workspace.getConfiguration(CONFIGURATION_SECTION);
const outputDir = buildUri(workspaceDir, conf.get('outputDirectory', '.VSCodeCounter'));
this.getCodeCounter()
.then(c => countLinesInDirectory(c, targetUri, outputDir, conf, this.toOutputChannel))
.then(results => outputResults(targetUri, results, outputDir, conf))
.catch(reason => showError(`countLinesInDirectory() failed.`, reason));
}

private onDidChangeWorkspaceFolders(e: vscode.WorkspaceFoldersChangeEvent) {
log(`onDidChangeWorkspaceFolders()`);
// e.added.forEach((f) => log(` added [${f.index}] ${f.name} : ${f.uri}`));
// e.removed.forEach((f) => log(` removed [${f.index}] ${f.name} : ${f.uri}`));
// vscode.workspace.workspaceFolders?.forEach((f) => log(` [${f.index}] ${f.name} : ${f.uri}`));
}
private onDidChangeActiveTextEditor(e: vscode.TextEditor|undefined) {
if (this.codeCounter_) {
log(`onDidChangeActiveTextEditor(${!e ? 'undefined' : e.document.uri})`);
this.countFile(e?.document);
}
}
private onDidChangeTextDocument(e: vscode.TextDocumentChangeEvent) {
if (this.codeCounter_) {
log(`onDidChangeTextDocument(${e.document.uri})`);
this.countFile(e.document);
}
}
private onDidChangeConfiguration() {
log(`onDidChangeConfiguration()`);
this.codeCounter_ = null;
this.countFile(vscode.window.activeTextEditor?.document);
}

public toggleVisible() {
if (!this.statusBarItem) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
this.countFile(vscode.window.activeTextEditor?.document);
private countLinesInEditor(editor: vscode.TextEditor|undefined) {
const doc = editor?.document;
if (!editor || !doc) {
this.showStatusBar(`${EXTENSION_NAME}:Unsupported`);
} else if (editor.selection.isEmpty) {
this.countLinesOfFile(doc);
} else {
this.statusBarItem.dispose();
this.statusBarItem = null;
}
}
private showStatusBar(text: string) {
if (this.statusBarItem) {
this.statusBarItem.show();
this.statusBarItem.text = text;
this.getCodeCounter().then(c =>{
const lineCounter = c.getById(doc.languageId) || c.getByUri(doc.uri);
if (lineCounter) {
const result = editor.selections
.map(s => lineCounter.count(doc.getText(s)))
.reduce((prev, cur) => {
return {
code: prev.code + cur.code,
comment: prev.comment + cur.comment,
blank: prev.blank + cur.blank,
};
}, {code:0, comment:0, blank:0});
this.showStatusBar(`Selected Code:${result.code} Comment:${result.comment} Blank:${result.blank}`);
} else {
this.showStatusBar(`${EXTENSION_NAME}:Unsupported`);
}
});
}
}
private countFile(doc: vscode.TextDocument|undefined) {
private countLinesOfFile(doc: vscode.TextDocument|undefined) {
if (!doc) {
this.showStatusBar(`${EXTENSION_NAME}:Unsupported`);
} else {
Expand All @@ -206,14 +232,19 @@ class CodeCounterController {
});
}
}
private showStatusBar(text: string) {
if (this.statusBarItem) {
this.statusBarItem.show();
this.statusBarItem.text = text;
}
}
private toOutputChannel(text: string) {
if (!this.outputChannel) {
this.outputChannel = vscode.window.createOutputChannel(EXTENSION_NAME);
}
this.outputChannel.show();
this.outputChannel.appendLine(text);
}

}
const encodingTable = new Map<string, string>([
['big5hkscs', 'big5-hkscs'],
Expand Down

0 comments on commit 7367271

Please sign in to comment.