Skip to content

Commit

Permalink
feat: Change the line counting method #96
Browse files Browse the repository at this point in the history
In line with the POSIX definition of lines in text files, lines that do not end with a newline (i.e., the last line) are not included in the count.
However, the default setting is the same as before to avoid confusion.
  • Loading branch information
uctakeoff committed Dec 16, 2023
1 parent e28d372 commit dfff05e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 65 deletions.
63 changes: 14 additions & 49 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,143 +7,108 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

* workspace counter in status bar.

## [3.2.0]

## [3.3.0]
### Changed
- Change the line counting method [#96](https://github.com/uctakeoff/vscode-counter/issues/96).
- In line with the POSIX definition of lines in text files, lines that do not end with a newline (i.e., the last line) are not included in the count.However, the default setting is the same as before to avoid confusion.

- Count the files in the top-level as a separate item #81
## [3.2.2]
### Fixed
- Fixed Issue [#93](https://github.com/uctakeoff/vscode-counter/issues/93).

## [3.1.0]
## [3.2.1]
### Fixed
- Added spaces in statusbar

## [3.2.0]
### Changed
- Count the files in the top-level as a separate item #81

## [3.1.0]
### Changed
- Support storing collected language elsewhere #78
## [3.0.0]

## [3.0.0]
### Changed

- Reviewed file detection rules

## [2.4.0]

### Added

- Counter diff output function

### Changed

- Moved the status bar to the right

### Removed

- Disuse Configuration : `VSCodeCounter.outputMarkdownSeparately`.

## [2.3.0]

### Fixed

- Error on large code bases

## [2.2.2]

### Fixed

- Fixed Issue [#48](https://github.com/uctakeoff/vscode-counter/issues/48).

## [2.2.1]

### Fixed

- Misconceptions about FileStat.

## [2.2.0]

### Added

- New Configuration : `VSCodeCounter.history`.

## [2.1.0]

### Added

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

## [2.0.0]

### Added

- New Command : `Save the collected language configurations`.

### Update

- This extension is no longer resident.
- Don't save whether or not the program is shown in the status bar in the settings.

## [1.3.5]

### Fixed

- Issue : `CSV could be more strictly formed`.

## [1.3.4]

### Fixed

- Issue : `Handling symlinks`.

## [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]

### Fixed

- Problems that occur when `files.encoding` is set to a value other than utf8

## [1.3.0]

### Added

- Support Multi-root Workspaces. (Selection type)


## [1.2.1]

### Fixed

- Update some modules.

## [1.2.0]

### Changed

- Output Markdown summary and details separately. (selectable by settings.json)


## [1.1.1]

### Added

- resolve file types using ["files.associations"](https://code.visualstudio.com/docs/languages/overview#_adding-a-file-extension-to-a-language) setting.


## [1.0.1]
### Fixed

- Error on large code bases

## [1.0.0]
### Fixed

- Auto ignore the .VSCodeCounter directory.

## [0.1.0]
Expand Down
7 changes: 6 additions & 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": "3.2.2",
"version": "3.3.0",
"publisher": "uctakeoff",
"author": {
"name": "Ushiyama Kentaro"
Expand Down Expand Up @@ -110,6 +110,11 @@
"type": "boolean",
"default": true
},
"VSCodeCounter.includeIncompleteLine": {
"description": "%configuration.includeIncompleteLine.description%",
"type": "boolean",
"default": true
},
"VSCodeCounter.endOfLine": {
"description": "%configuration.endOfLine.description%",
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions package.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"configuration.maxOpenFiles.description": "VSCodeCounter が同時に読み取ることのできるファイルの最大数.",
"configuration.printNumberWithCommas.description": "数値をカンマ区切りで出力する (CSVを除く).",
"configuration.ignoreUnsupportedFile.description": "サポートしないファイルを無視する.",
"configuration.includeIncompleteLine.description": "改行で終わらない行(最終行)を含める.",
"configuration.endOfLine.description": "出力ファイルで使用する改行文字.",
"configuration.exclude.description": "ファイルとフォルダーを除外するための glob パターン.",
"configuration.include.description": "ファイルとフォルダーを追加するための glob パターン.",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"configuration.maxOpenFiles.description": "Maximum number of files that VSCodeCounter can read simultaneously.",
"configuration.printNumberWithCommas.description": "Whether to print a number with commas as thousands separators.(except for CSV)",
"configuration.ignoreUnsupportedFile.description": "Ignore unsupported files.",
"configuration.includeIncompleteLine.description": "Include lines that do not end in a newline (last line).",
"configuration.endOfLine.description": "A new line character to be used in the output file.",
"configuration.exclude.description": "Configure glob patterns for excluding files and folders.",
"configuration.include.description": "Configure glob patterns for including files and folders.",
Expand Down
12 changes: 8 additions & 4 deletions src/LineCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ export class LineCounter {
private lineComments: string[],
private blockComments: [string, string][],
private blockStrings: [string, string][]) {
this.blockCommentBegins = this.blockComments.map(b => b[0]);
this.blockStringBegins = this.blockStrings.map(b => b[0]);
this.blockCommentBegins = this.blockComments.map(b => b[0]);
this.blockStringBegins = this.blockStrings.map(b => b[0]);
}
public count(text: string): Count {
public count(text: string, includeIncompleteLine = false): Count {
let result = [0, 0, 0];
let blockCommentEnd = '';
let blockStringEnd = '';
text.split(/\r\n|\r|\n/).map(line => line.trim()).forEach((line, lineIndex) => {
const lines = text.split(/\r\n|\r|\n/).map(line => line.trim());
if (!includeIncompleteLine) {
lines.pop();
}
lines.forEach((line, lineIndex) => {
let type = (blockCommentEnd.length > 0) ? LineType.Comment : (blockStringEnd.length > 0) ? LineType.Code : LineType.Blank;
let i = 0;
while (i < line.length) {
Expand Down
34 changes: 23 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const loadConfig = () => {
history: Math.max(1, conf.get('history', 5)),
languages: conf.get<{ [key: string]: Partial<LanguageConf> }>('languages', {}),

includeIncompleteLine: conf.get('includeIncompleteLine', false),
endOfLine: conf.get('endOfLine', '\n'),
printNumberWithCommas: conf.get('printNumberWithCommas', true),
outputPreviewType: conf.get<string>('outputPreviewType', ''),
Expand Down Expand Up @@ -230,7 +231,13 @@ class CodeCounterController {
}

const counter = await this.getCodeCounter();
const results = await countLines(counter, targetFiles, this.conf.maxOpenFiles, this.conf.encoding, this.conf.ignoreUnsupportedFile, (msg: string) => statusBar.text = `VSCodeCounter: ${msg}`);
const results = await countLines(counter, targetFiles, {
maxOpenFiles: this.conf.maxOpenFiles,
fileEncoding: this.conf.encoding,
ignoreUnsupportedFile: this.conf.ignoreUnsupportedFile,
includeIncompleteLine: this.conf.includeIncompleteLine,
showStatus: (msg: string) => statusBar.text = `VSCodeCounter: ${msg}`
});
if (results.length <= 0) {
throw Error(`There was no target file.`);
}
Expand Down Expand Up @@ -267,7 +274,7 @@ class CodeCounterController {
const lineCounter = c.getCounter(doc.uri.fsPath, doc.languageId);
if (lineCounter) {
const result = editor.selections
.map(s => lineCounter.count(doc.getText(s)))
.map(s => lineCounter.count(doc.getText(s), this.conf.includeIncompleteLine))
.reduce((prev, cur) => prev.add(cur), new Count());
this.showStatusBar(`Selected Code: ${result.code} Comment: ${result.comment} Blank: ${result.blank}`);
} else {
Expand All @@ -282,7 +289,7 @@ class CodeCounterController {
const c = await this.getCodeCounter();
const lineCounter = c.getCounter(doc.uri.fsPath, doc.languageId);
if (lineCounter) {
const result = lineCounter?.count(doc.getText());
const result = lineCounter?.count(doc.getText(), this.conf.includeIncompleteLine);
this.showStatusBar(`Code: ${result.code} Comment: ${result.comment} Blank: ${result.blank}`);
} else {
this.showStatusBar();
Expand Down Expand Up @@ -311,15 +318,21 @@ const loadGitIgnore = async () => {
const values = await readUtf8Files(gitignoreFiles.sort());
return new Gitignore('').merge(...values.map(p => new Gitignore(p.data, dirUri(p.uri).fsPath)));
}

const countLines = (lineCounterTable: LineCounterTable, fileUris: vscode.Uri[], maxOpenFiles: number, fileEncoding: string, ignoreUnsupportedFile: boolean, showStatus: (text: string) => void) => {
type CountLineOption = {
maxOpenFiles: number;
fileEncoding: string;
ignoreUnsupportedFile?: boolean;
includeIncompleteLine?: boolean;
showStatus?: (text: string) => void;
};
const countLines = (lineCounterTable: LineCounterTable, fileUris: vscode.Uri[], option: CountLineOption) => {
log(`countLines : target ${fileUris.length} files`);
return new Promise(async (resolve: (value: Result[]) => void, reject: (reason: string) => void) => {
const results: Result[] = [];
if (fileUris.length <= 0) {
resolve(results);
}
const decoder = createTextDecoder(fileEncoding);
const decoder = createTextDecoder(option.fileEncoding);
const totalFiles = fileUris.length;
let fileCount = 0;
const onFinish = () => {
Expand All @@ -333,16 +346,15 @@ const countLines = (lineCounterTable: LineCounterTable, fileUris: vscode.Uri[],
const fileUri = fileUris[i];
const lineCounter = lineCounterTable.getCounter(fileUri.fsPath);
if (lineCounter) {

while ((i - fileCount) >= maxOpenFiles) {
while ((i - fileCount) >= option.maxOpenFiles) {
// log(`sleep : total:${totalFiles} current:${i} finished:${fileCount} valid:${results.length}`);
showStatus(`${fileCount}/${totalFiles}`);
option.showStatus?.(`${fileCount}/${totalFiles}`);
await sleep(50);
}

vscode.workspace.fs.readFile(fileUri).then(data => {
try {
results.push(new Result(fileUri, lineCounter.name, lineCounter.count(decoder.decode(data))));
results.push(new Result(fileUri, lineCounter.name, lineCounter.count(decoder.decode(data), option.includeIncompleteLine)));
} catch (e: any) {
log(`"${fileUri}" Read Error : ${e.message}.`);
results.push(new Result(fileUri, '(Read Error)'));
Expand All @@ -355,7 +367,7 @@ const countLines = (lineCounterTable: LineCounterTable, fileUris: vscode.Uri[],
onFinish();
});
} else {
if (!ignoreUnsupportedFile) {
if (!option.ignoreUnsupportedFile) {
results.push(new Result(fileUri, '(Unsupported)'));
}
onFinish();
Expand Down

0 comments on commit dfff05e

Please sign in to comment.