Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/extAlias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

// a file extension alias mapping used when config 'assumeFilename' is empty
export const EXT_ALIAS = {
'.cjs': '.js',
'.cts': '.ts',
'.mts': '.ts',
};
33 changes: 18 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import cp = require('child_process');
import path = require('path');
import {MODES,
ALIAS} from './clangMode';
import {EXT_ALIAS} from './extAlias';
import {getBinPath} from './clangPath';
import sax = require('sax');

export let outputChannel = vscode.window.createOutputChannel('Clang-Format');

function getPlatformString() {
switch(process.platform) {
case 'win32': return 'windows';
case 'linux': return 'linux';
case 'darwin': return 'osx';
switch (process.platform) {
case 'win32': return 'windows';
case 'linux': return 'linux';
case 'darwin': return 'osx';
}

return 'unknown';
Expand Down Expand Up @@ -96,7 +97,6 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
default:
reject(`Unexpected tag ${tag.name}`);
}

};

parser.ontext = (text) => {
Expand All @@ -123,7 +123,6 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma

parser.write(xml);
parser.end();

});
}

Expand Down Expand Up @@ -185,28 +184,32 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma

private getAssumedFilename(document: vscode.TextDocument) {
let assumedFilename = vscode.workspace.getConfiguration('clang-format').get<string>('assumeFilename');
let parsedPath = path.parse(document.fileName);
let fileNoExtension = path.join(parsedPath.dir, parsedPath.name);
if (assumedFilename === '') {
const extAlias = EXT_ALIAS[parsedPath.ext];
if (extAlias) {
return fileNoExtension + extAlias;
}
return document.fileName;
}
let parsedPath = path.parse(document.fileName);
let fileNoExtension = path.join(parsedPath.dir, parsedPath.name);
return assumedFilename
.replace(/\${file}/g, document.fileName)
.replace(/\${fileNoExtension}/g, fileNoExtension)
.replace(/\${fileBasename}/g, parsedPath.base)
.replace(/\${fileBasenameNoExtension}/g, parsedPath.name)
.replace(/\${fileExtname}/g, parsedPath.ext);
.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 getWorkspaceFolder(): string|undefined {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage("Unable to get the location of clang-format executable - no active workspace selected");
vscode.window.showErrorMessage('Unable to get the location of clang-format executable - no active workspace selected');
return undefined;
}

if (!vscode.workspace.workspaceFolders) {
vscode.window.showErrorMessage("Unable to get the location of clang-format executable - no workspaces available");
vscode.window.showErrorMessage('Unable to get the location of clang-format executable - no workspaces available');
return undefined
}

Expand Down