Skip to content
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

#2611: make LSP config available synchronously #2676

Open
wants to merge 1 commit into
base: dev
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
18 changes: 9 additions & 9 deletions src/calva-fmt/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const FormatDepthDefaults = {
defprotocol: 2,
};

export async function indentPosition(position: vscode.Position, document: vscode.TextDocument) {
export /*async*/ function indentPosition(position: vscode.Position, document: vscode.TextDocument) {
const editor = util.getActiveTextEditor();
const pos = new vscode.Position(position.line, 0);
const indent = getIndent(
getDocument(document).model.lineInputModel,
getDocumentOffset(document, position),
await config.getConfig(document)
config.getConfig(document)
);
const newPosition = new vscode.Position(position.line, indent);
const delta = document.lineAt(position.line).firstNonWhitespaceCharacterIndex - indent;
Expand Down Expand Up @@ -97,7 +97,7 @@ export async function formatRange(document: vscode.TextDocument, range: vscode.R
return vscode.workspace.applyEdit(wsEdit);
}

export async function formatPositionInfo(
export /*async*/ function formatPositionInfo(
editor: vscode.TextEditor,
onType: boolean = false,
extraConfig: CljFmtConfig = {}
Expand All @@ -122,7 +122,7 @@ export async function formatPositionInfo(
_convertEolNumToStringNotation(doc.eol),
onType,
{
...(await config.getConfig()),
...config.getConfig(),
...extraConfig,
'comment-form?': cursor.getFunctionName() === 'comment',
}
Expand Down Expand Up @@ -207,7 +207,7 @@ export async function formatPosition(
extraConfig: CljFmtConfig = {}
): Promise<boolean> {
const doc: vscode.TextDocument = editor.document,
formattedInfo = await formatPositionInfo(editor, onType, extraConfig);
formattedInfo = formatPositionInfo(editor, onType, extraConfig);
if (formattedInfo && formattedInfo.previousText != formattedInfo.formattedText) {
return editor
.edit(
Expand Down Expand Up @@ -262,11 +262,11 @@ export function trimWhiteSpacePositionCommand(editor: vscode.TextEditor) {
void formatPosition(editor, false, { 'remove-multiple-non-indenting-spaces?': true });
}

export async function formatCode(code: string, eol: number) {
export /*async*/ function formatCode(code: string, eol: number) {
const d = {
'range-text': code,
eol: _convertEolNumToStringNotation(eol),
config: await config.getConfig(),
config: config.getConfig(),
};
const result = jsify(formatText(d));
if (!result['error']) {
Expand All @@ -277,7 +277,7 @@ export async function formatCode(code: string, eol: number) {
}
}

async function _formatRange(
/*async*/ function _formatRange(
rangeText: string,
allText: string,
range: number[],
Expand All @@ -288,7 +288,7 @@ async function _formatRange(
'all-text': allText,
range: range,
eol: eol,
config: await config.getConfig(),
config: config.getConfig(),
};
const result = jsify(formatTextAtRange(d));
if (!result['error']) {
Expand Down
50 changes: 37 additions & 13 deletions src/formatter-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,52 @@ function getConfigPath(workspaceConfig: vscode.WorkspaceConfiguration): string |

export type FormatterConfig = Partial<Awaited<ReturnType<typeof getConfig>>>;

export async function getConfig(
let cachedLspConfig = undefined;

async function getAndCacheLspConfig(documentUri: vscode.Uri) {
const clientProvider = lsp.getClientProvider();
const client = clientProvider.getClientForDocumentUri(documentUri);
if (client && client.isRunning) {
lspFormatConfig = await lsp.api.getCljFmtConfig(client);
if (lspFormatConfig) {
cachedLspConfig = lspFormatConfig;
} else {
console.error(
'Fetching formatting settings from clojure-lsp failed. Check that you are running a version of clojure-lsp that provides "cljfmt-raw" in serverInfo.'
);
}
return lspFormatConfig;
}
}

function getCachedLspConfig(documentUri: vscode.Uri) {
// The Uri ensures LSP config could be cached per-document, if that were necessary.
if (cachedLspConfig) {
return cachedLspConfig;
} else {
console.log(
'LSP config is not cached yet, but I will get right on it. Try again in a little while.'
);
void getAndCacheLspConfig(documentUri);
return {};
}
}

// To enable synchronous commands to get an instant configuration,
// might return undefined for LSP-based members until background query completes.
export function getConfig(
document: vscode.TextDocument = vscode.window.activeTextEditor?.document
): Promise<{
): {
'format-as-you-type': boolean;
'keep-comment-forms-trail-paren-on-own-line?': boolean;
'cljfmt-options-string': string;
'cljfmt-options': object;
}> {
} {
const workspaceConfig = vscode.workspace.getConfiguration('calva.fmt');
const configPath: string | undefined = getConfigPath(workspaceConfig);

if (configPath === LSP_CONFIG_KEY && document) {
const clientProvider = lsp.getClientProvider();
const client = clientProvider.getClientForDocumentUri(document.uri);
if (client && client.isRunning) {
lspFormatConfig = await lsp.api.getCljFmtConfig(client);
if (!lspFormatConfig) {
console.error(
'Fetching formatting settings from clojure-lsp failed. Check that you are running a version of clojure-lsp that provides "cljfmt-raw" in serverInfo.'
);
}
}
lspFormatConfig = getCachedLspConfig(document.uri);
}
const cljfmtContent: string | undefined =
configPath === LSP_CONFIG_KEY
Expand Down
2 changes: 1 addition & 1 deletion src/paredit/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ const pareditCommands = [
{
command: 'paredit.deleteBackward',
handler: async (doc: EditableDocument) => {
await paredit.backspace(doc, await config.getConfig());
await paredit.backspace(doc, config.getConfig());
},
},
{
Expand Down