MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / formatActiveFileWithPrettier

Function formatActiveFileWithPrettier

src/lib/prettierFormatter.js:73–113  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

71};
72
73export async function formatActiveFileWithPrettier() {
74 const file = editorManager?.activeFile;
75 const editor = editorManager?.editor;
76 if (!file || file.type !== "editor" || !editor) return false;
77
78 const modeName = (file.currentMode || "text").toLowerCase();
79 const parser = getParserForMode(modeName);
80 if (!parser) {
81 toast("Prettier does not support this file type yet");
82 return false;
83 }
84
85 const doc = editor.state.doc;
86 const source = doc.toString();
87 const filepath = file.uri || file.filename || "";
88 try {
89 const config = await resolvePrettierConfig(file);
90 const formatted = await prettier.format(source, {
91 ...config,
92 parser,
93 plugins: PRETTIER_PLUGINS,
94 filepath,
95 overrideEditorconfig: true,
96 });
97
98 if (formatted === source) return true;
99
100 editor.dispatch({
101 changes: {
102 from: 0,
103 to: doc.length,
104 insert: formatted,
105 },
106 });
107 return true;
108 } catch (error) {
109 const message = error instanceof Error ? error.message : String(error);
110 toast(message);
111 return false;
112 }
113}
114
115function getParserForMode(modeName) {
116 if (MODE_TO_PARSER[modeName]) return MODE_TO_PARSER[modeName];

Callers 1

Calls 5

getParserForModeFunction · 0.85
resolvePrettierConfigFunction · 0.85
StringInterface · 0.85
formatMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected