MCPcopy Create free account
hub / github.com/compiler-explorer/compiler-explorer / formatCurrentText

Method formatCurrentText

static/panes/editor.ts:1199–1253  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1197 }
1198
1199 formatCurrentText(): void {
1200 const previousSource = this.getSource();
1201 const lang = this.currentLanguage;
1202
1203 if (!Object.prototype.hasOwnProperty.call(lang, 'formatter')) {
1204 this.alertSystem.notify('This language does not support in-editor formatting', {
1205 group: 'formatting',
1206 alertClass: 'notification-error',
1207 });
1208 return;
1209 }
1210
1211 $.ajax({
1212 type: 'POST',
1213 url: window.location.origin + this.httpRoot + 'api/format/' + lang?.formatter,
1214 dataType: 'json', // Expected
1215 contentType: 'application/json', // Sent
1216 data: JSON.stringify({
1217 source: previousSource,
1218 base: this.settings.formatBase,
1219 }),
1220 success: result => {
1221 if (result.exit === 0) {
1222 if (this.doesMatchEditor(previousSource)) {
1223 this.updateSource(result.answer);
1224 } else {
1225 this.confirmOverwrite(this.updateSource.bind(this, result.answer));
1226 }
1227 } else {
1228 // Ops, the formatter itself failed!
1229 this.alertSystem.notify('We encountered an error formatting your code: ' + result.answer, {
1230 group: 'formatting',
1231 alertClass: 'notification-error',
1232 });
1233 }
1234 },
1235 error: (xhr, e_status, error) => {
1236 // Hopefully we have not exploded!
1237 if (xhr.responseText) {
1238 try {
1239 const res = JSON.parse(xhr.responseText);
1240 error = res.answer || error;
1241 } catch {
1242 // continue regardless of error
1243 }
1244 }
1245 error = error || 'Unknown error';
1246 this.alertSystem.notify('We ran into some issues while formatting your code: ' + error, {
1247 group: 'formatting',
1248 alertClass: 'notification-error',
1249 });
1250 },
1251 cache: true,
1252 });
1253 }
1254
1255 override resize(): void {
1256 super.resize();

Callers

nothing calls this directly

Calls 6

getSourceMethod · 0.95
doesMatchEditorMethod · 0.95
updateSourceMethod · 0.95
confirmOverwriteMethod · 0.95
notifyMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected