aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMats Larsen <me@supergrecko.com>2021-11-01 20:36:25 +0100
committerMats Larsen <me@supergrecko.com>2021-11-01 20:36:25 +0100
commitd289ddffd626bffb554248059536ef35ca77f9c0 (patch)
tree178b05aebc2816a7115f72fa7f32b8fcd3774fe4
parent07a3181e18e2759ca73792070599f285dc4b947c (diff)
downloadcompiler-explorer-gh-1232.tar.gz
compiler-explorer-gh-1232.zip
Actually use the setting style in the format apigh-1232
-rw-r--r--static/formatter-registry.interfaces.ts9
-rw-r--r--static/formatter-registry.ts33
-rw-r--r--static/settings.interfaces.ts2
3 files changed, 34 insertions, 10 deletions
diff --git a/static/formatter-registry.interfaces.ts b/static/formatter-registry.interfaces.ts
index 4994c7ead..c60aaa188 100644
--- a/static/formatter-registry.interfaces.ts
+++ b/static/formatter-registry.interfaces.ts
@@ -22,10 +22,17 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
+import { FormatBase } from './settings.interfaces';
+
export interface FormatRequestOptions {
source: string;
formatterId: string;
- base: string;
+ /**
+ * Specifies which formatting preset to use. "__DefaultStyle" means the
+ * formatter doesn't have style presets, and that the backend should
+ * ignore the "base" option.
+ */
+ base: FormatBase | '__DefaultStyle';
tabWidth: number;
useSpaces: boolean;
}
diff --git a/static/formatter-registry.ts b/static/formatter-registry.ts
index b014cd8dd..d7224002c 100644
--- a/static/formatter-registry.ts
+++ b/static/formatter-registry.ts
@@ -56,14 +56,22 @@ const getFormattedCode = async ({ source, formatterId, base, tabWidth, useSpaces
}
// We had an error (either HTTP request error, or API error)
// Figure out which it is, show it to the user, and reject the promise
- const cause = (res.status === 200) ? body.answer : res.statusText;
+ const cause = body?.answer ?? res.statusText
throw new Error(cause);
};
+/**
+ * Create a monaco DocumentFormattingEditProvider for a registered monaco
+ * language.
+ *
+ * @param language The monaco-editor registered language to format code for
+ * @param formatter The CE format API backend to use
+ * @param isOneTrueStyle Whether the CE format API backend has one true style
+ */
const getDocumentFormatter = (
language: string,
formatter: string,
- formatBase: string,
+ isOneTrueStyle: boolean,
): monaco.languages.DocumentFormattingEditProvider => ({
async provideDocumentFormattingEdits(
model: monaco.editor.ITextModel,
@@ -71,14 +79,17 @@ const getDocumentFormatter = (
token: monaco.CancellationToken,
): Promise<monaco.languages.TextEdit[]> {
const settings: SiteSettings = getStoredSettings();
+ // If there is only one style, return __DefaultStyle.
+ const base = isOneTrueStyle ? '__DefaultStyle' : settings.formatBase;
const source = model.getValue();
- // Request the formatted code. If that API call fails, we just back off and return the user's old code.
+ // Request the formatted code. If that API call fails, we just back off
+ // and return the user's old code.
const formattedSource = await getFormattedCode({
formatterId: formatter,
- base: formatBase,
tabWidth: settings.tabWidth,
useSpaces: settings.useSpaces,
source,
+ base,
}).catch(err => onFormatError(err, source));
return [{
range: model.getFullModelRange(),
@@ -87,7 +98,13 @@ const getDocumentFormatter = (
},
});
-monaco.languages.registerDocumentFormattingEditProvider('cppp', getDocumentFormatter('cppp', 'clangformat', 'Google'));
-monaco.languages.registerDocumentFormattingEditProvider('go', getDocumentFormatter('go', 'gofmt', 'None'));
-monaco.languages.registerDocumentFormattingEditProvider('nc', getDocumentFormatter('nc', 'clangformat', 'Google'));
-monaco.languages.registerDocumentFormattingEditProvider('rust', getDocumentFormatter('rust', 'rustfmt', 'None'));
+/** Register a monaco-editor language's default document formatting provider */
+const register = (lang: string, formatter: string, isOneTrueStyle: boolean) => {
+ const provider = getDocumentFormatter(lang, formatter, isOneTrueStyle);
+ monaco.languages.registerDocumentFormattingEditProvider(lang, provider);
+}
+
+register('cppp', 'clangformat', false);
+register('nc', 'clangformat', false);
+register('go', 'gofmt', true);
+register('rust', 'rustfmt', true);
diff --git a/static/settings.interfaces.ts b/static/settings.interfaces.ts
index 481428960..eaf45983d 100644
--- a/static/settings.interfaces.ts
+++ b/static/settings.interfaces.ts
@@ -30,7 +30,7 @@ type ColourScheme =
| 'gray-shade'
| 'rainbow-dark';
-type FormatBase =
+export type FormatBase =
| 'Google'
| 'LLVM'
| 'Mozilla'