Skip to content

Add config file path option to package.json #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@
"SQL-Formatter-VSCode.paramTypes": {
"type": "object",
"markdownDescription": "Specifies parameter placeholders types to support."
},
"SQL-Formatter-VSCode.configFilePath": {
"type": "string",
"markdownDescription": "Specifies the path to the configuration file to configure sql-formatter; takes precedence over other settings."
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FormatOptionsWithLanguage,
FormatOptions,
} from 'sql-formatter';
import { validateConfig } from 'sql-formatter/dist/cjs/validateConfig';

type ParamTypes = FormatOptions['paramTypes'];

Expand Down Expand Up @@ -53,3 +54,19 @@ const createIndentationConfig = (
};
}
};

export const getConfigFromPath = async (
configFilePath: string,
): Promise<FormatOptionsWithLanguage | undefined> => {
try {
const fileContent = await vscode.workspace.fs.readFile(vscode.Uri.file(configFilePath));
const configString = Buffer.from(fileContent).toString('utf8');
const parsed = JSON.parse(configString);
return validateConfig(parsed);
} catch (error) {
vscode.window.showErrorMessage(
`Unable to read config file from path ${configFilePath}:\n${error}`,
);
return undefined;
}
};
21 changes: 13 additions & 8 deletions src/formatSelection.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import * as vscode from 'vscode';
import { createConfig } from './config';
import { createConfig, getConfigFromPath } from './config';
import { sqlDialects } from './sqlDialects';
import { formatEditorText } from './formatEditorText';

export function formatSelection() {
export async function formatSelection() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}

try {
replaceEachSelection(editor, text => formatEditorText(text, createConfigForEditor(editor)));
const config = await createConfigForEditor(editor);
replaceEachSelection(editor, text => formatEditorText(text, config));
} catch (e) {
vscode.window.showErrorMessage('Unable to format SQL:\n' + e);
}
Expand All @@ -22,12 +23,16 @@ function replaceEachSelection(editor: vscode.TextEditor, fn: (code: string) => s
});
}

const createConfigForEditor = (editor: vscode.TextEditor) =>
createConfig(
vscode.workspace.getConfiguration('SQL-Formatter-VSCode'),
editorFormattingOptions(editor),
detectSqlDialect(editor),
const createConfigForEditor = async (editor: vscode.TextEditor) => {
const extensionConfiguration = vscode.workspace.getConfiguration('SQL-Formatter-VSCode');
const configFilePath = extensionConfiguration.get<string>('configFilePath');
const configFromPath = configFilePath ? await getConfigFromPath(configFilePath) : undefined;
const formattingOptions = editorFormattingOptions(editor);
const detectedSqlDialect = detectSqlDialect(editor);
return (
configFromPath ?? createConfig(extensionConfiguration, formattingOptions, detectedSqlDialect)
);
};

const detectSqlDialect = (editor: vscode.TextEditor) =>
sqlDialects[editor.document.languageId] ?? 'sql';
Expand Down