Skip to content

Commit

Permalink
Merge pull request #2 from somethingsoftware/dev/assumefilename
Browse files Browse the repository at this point in the history
Change "Assume Filename" to be per language
  • Loading branch information
Mr-Bossman authored Oct 25, 2024
2 parents 5aca008 + 33761ff commit 6d3ac07
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,21 @@
],
"uniqueItems": true
},
"clang-format.assumeFilename": {
"order": 99,
"clang-format.assumeFilename.default": {
"order": 98,
"type": "string",
"default": "",
"description": "When reading from stdin, clang-format assumes this filename to look for a style config file (with -style=file) and to determine the language.\nThe default value is the current filename being formated."
},
"clang-format.assumeFilename.languages": {
"order": 99,
"type": "object",
"default": {},
"additionalProperties": {
"type": "string"
},
"description": "Object with the key as LanguageId and value as the assumeFilename."
},
"clang-format.additionalArguments": {
"order": 100,
"type": "string",
Expand Down
33 changes: 25 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,41 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
});
}

private getAssumedFilename(document: vscode.TextDocument) {
const config = vscode.workspace.getConfiguration('clang-format');
const assumedFilename = config.get<string>('assumeFilename');
private replaceAssumedFilenameVariables(str: string, document: vscode.TextDocument): string {
const parsedPath = path.parse(document.fileName);
const fileNoExtension = path.join(parsedPath.dir, parsedPath.name);

if (assumedFilename === '' || assumedFilename === null) {
return document.fileName;
}

return assumedFilename
return str
.replace(/\${file}/g, document.fileName)
.replace(/\${fileNoExtension}/g, fileNoExtension)
.replace(/\${fileBasename}/g, parsedPath.base)
.replace(/\${fileBasenameNoExtension}/g, parsedPath.name)
.replace(/\${fileExtname}/g, parsedPath.ext);
}

private getAssumedFilename(document: vscode.TextDocument) {
const config = vscode.workspace.getConfiguration('clang-format');
const assumedFilename = config.get<object>('assumeFilename.languages');
let ret = assumedFilename[this.getLanguage(document)];

if (ret && ret.trim()) {
ret = this.replaceAssumedFilenameVariables(ret.trim(), document);
if (ret && ret.trim()) {
return ret.trim();
}
}

ret = config.get<string>('assumeFilename.default');
if (ret && ret.trim()) {
ret = this.replaceAssumedFilenameVariables(ret.trim(), document);
if (ret && ret.trim()) {
return ret.trim();
}
}

return this.replaceStyleVariables(this.defaultConfigure.style, document);
}

private getWorkspaceFolder(): string | undefined {
const editor = vscode.window.activeTextEditor;
if (!editor) {
Expand Down

0 comments on commit 6d3ac07

Please sign in to comment.