Skip to content

Commit

Permalink
feat #78
Browse files Browse the repository at this point in the history
  • Loading branch information
uctakeoff committed Oct 23, 2022
1 parent 053d3b2 commit 32b043e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
9 changes: 7 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": "3.0.5",
"version": "3.1.0",
"publisher": "uctakeoff",
"author": {
"name": "Ushiyama Kentaro"
Expand Down Expand Up @@ -187,10 +187,15 @@
"enum": [
"global settings",
"workspace settings",
"output directory"
"output directory",
"use languageConfUri"
],
"default": "global settings"
},
"VSCodeCounter.languageConfUri": {
"description": "%configuration.languageConfUri.description%",
"type": "string"
},
"VSCodeCounter.languages": {
"description": "%configuration.languages.description%",
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions package.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"configuration.outputAsMarkdown.description": "結果をMarkdown形式で出力する.",
"configuration.outputPreviewType.description": "カウント後にプレビューする出力ファイルの種類.",
"configuration.saveLocation.description": "集めた言語設定を保存する場所を指定する.",
"configuration.languageConfUri.description": "saveLocation で \"use languageConfUri\" を指定した場合に用いる保存先ファイルパスまたはURI.",
"configuration.languages.description": "保存された言語設定.",
"configuration.languages.aliases.description": "言語の別名.",
"configuration.languages.filenames.description": "言語に関連付けられたファイル名.",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"configuration.outputAsMarkdown.description": "Whether to output the result as a Markdown file.",
"configuration.outputPreviewType.description": "Type of output file to preview after counting.",
"configuration.saveLocation.description": "Specify where to store the collected language configurations.",
"configuration.languageConfUri.description": "The destination file path or URI to be used if saveLocation \"use languageConfUri\" is specified.",
"configuration.languages.description": "Saved Language Configurations.",
"configuration.languages.aliases.description": "Name aliases for the language.",
"configuration.languages.filenames.description": "Filenames associated to the language.",
Expand Down
24 changes: 18 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from 'path';
import { Count } from './LineCounter';
import { LanguageConf, LineCounterTable } from './LineCounterTable';
import Gitignore from './Gitignore';
import { buildUri, createTextDecoder, currentWorkspaceFolder, dirUri, makeDirectories, readJsonFile, readUtf8Files, showTextPreview, writeTextFile } from './vscode-utils';
import { buildUri, createTextDecoder, currentWorkspaceFolder, dirUri, makeDirectories, parseUriOrFile, readJsonFile, readUtf8Files, showTextPreview, writeTextFile } from './vscode-utils';
import { internalDefinitions } from './internalDefinitions';

const EXTENSION_ID = 'uctakeoff.vscode-counter';
Expand Down Expand Up @@ -55,6 +55,8 @@ export const activate = (context: vscode.ExtensionContext) => {
// this method is called when your extension is deactivated
export const deactivate = () => { }

type LanguageLocation = 'global settings' | 'workspace settings' | 'output directory' | 'use languageConfUri';

const loadConfig = () => {
const conf = vscode.workspace.getConfiguration(CONFIGURATION_SECTION);
const confFiles = vscode.workspace.getConfiguration("files", null);
Expand All @@ -66,8 +68,9 @@ const loadConfig = () => {
}
return {
configuration: conf,
saveLocation: conf.get<string>('saveLocation', 'global settings'),
saveLocation: conf.get<string>('saveLocation', 'global settings') as LanguageLocation,
outputDirectory: conf.get('outputDirectory', '.VSCodeCounter'),
languageConfUri: conf.get('languageConfUri', ''),
// include: `{${include.join(',')}}`,
// exclude: `{${exclude.join(',')}}`,
include: include.join(','),
Expand Down Expand Up @@ -437,13 +440,19 @@ const saveLanguageConfigurations = async (langs: { [key: string]: LanguageConf }
case "workspace settings":
conf.configuration.update('languages', langs, vscode.ConfigurationTarget.Workspace);
break;
case "output directory":
case "output directory":{
const workFolder = await currentWorkspaceFolder();
const outputDir = buildUri(workFolder.uri, conf.outputDirectory);
await makeDirectories(outputDir);
await writeTextFile(outputDir, 'languages.json', JSON.stringify(langs));
break;
default: break;
}
case "use languageConfUri":{
const workFolder = await currentWorkspaceFolder();
await writeTextFile(parseUriOrFile(conf.languageConfUri, workFolder.uri), undefined, JSON.stringify(langs));
break;
}
default: break;
}
}

Expand All @@ -457,10 +466,14 @@ const loadLanguageConfigurations = async (conf: Config): Promise<{ [key: string]
const workFolder = await currentWorkspaceFolder();
const outputDir = buildUri(workFolder.uri, conf.outputDirectory);
return await readJsonFile<{ [key: string]: Partial<LanguageConf> }>(outputDir, 'languages.json', {});
case "use languageConfUri":{
const workFolder = await currentWorkspaceFolder();
return await readJsonFile<{ [key: string]: Partial<LanguageConf> }>(parseUriOrFile(conf.languageConfUri, workFolder.uri), undefined, {});
}
default: break;
}
} catch (e: any) {
log(`loadLanguageConfigurations failed. ${e.message}`);
showError(`loadLanguageConfigurations failed. ${e.message}`);
}
return {};
}
Expand Down Expand Up @@ -566,7 +579,6 @@ class Statistics extends Count {
return super.add(value);
}
}

class MarkdownTableFormatter {
private valueToString: (obj: any) => string;
private columnInfo: { title: string, format: string }[];
Expand Down
12 changes: 9 additions & 3 deletions src/vscode-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export const currentWorkspaceFolder = async () => {

export const buildUri = (uri: vscode.Uri, ...names: string[]) => uri.with({ path: `${uri.path}/${names.join('/')}` });
export const dirUri = (uri: vscode.Uri) => uri.with({ path: path.dirname(uri.path) });

export const parseUriOrFile = (uriOrFileath: string, baseUri?: vscode.Uri) => {
const u = vscode.Uri.parse(uriOrFileath);
// log(uriOrFileath, u.toString(), path.isAbsolute(uriOrFileath), baseUri?.toString(), buildUri(baseUri??u, uriOrFileath).fsPath, (!baseUri || path.isAbsolute(uriOrFileath)));
return uriOrFileath.startsWith(u.scheme + ':/') ? u
: (!baseUri || path.isAbsolute(uriOrFileath)) ? vscode.Uri.file(uriOrFileath)
: buildUri(baseUri, uriOrFileath);
}
const decoderU8 = new TextDecoder('utf8');
const encoderU8 = new TextEncoder();

Expand Down Expand Up @@ -153,8 +159,8 @@ export const showTextPreview = async (uri: vscode.Uri) => {
await showTextFile(uri);
}
}
export const writeTextFile = async (baseUri: vscode.Uri, path: string, text: string) => {
const uri = buildUri(baseUri, path);
export const writeTextFile = async (baseUri: vscode.Uri, path: string | undefined, text: string) => {
const uri = path ? buildUri(baseUri, path) : baseUri;
// log(`writeTextFile : ${uri} ${text.length}B`);
await vscode.workspace.fs.writeFile(uri, encoderU8.encode(text));
return uri;
Expand Down

0 comments on commit 32b043e

Please sign in to comment.