@lvce-editor/editor-worker 19.76.3 → 19.77.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -120,6 +120,7 @@ export interface EditorState {
120
120
  readonly fontSize: number;
121
121
  readonly fontWeight: number;
122
122
  readonly forceText?: boolean;
123
+ readonly formatActions?: "pre-save" | "post-save";
123
124
  readonly formatOnSave?: boolean;
124
125
  readonly gutterDecorations: readonly EditorGutterDecoration[];
125
126
  readonly gutterWidth: number;
@@ -175,6 +176,7 @@ export interface EditorState {
175
176
  readonly outerHeight?: number;
176
177
  readonly outerWidth: number;
177
178
  readonly platform: number;
179
+ readonly postSaveFormat?: boolean;
178
180
  readonly primarySelectionIndex: number;
179
181
  readonly problemNavigationDiagnostic?: any;
180
182
  readonly problemsHighlightedRow: number;
package/dist/diff-sdk.js CHANGED
@@ -4112,6 +4112,7 @@ const mergeLinksWithDiagnosticDecorations = (editor, diagnosticDecorations) => {
4112
4112
  const requestGenerations = new Map();
4113
4113
  const requestDiagnosticSnapshots = new Map();
4114
4114
  const previousProviderResults = new Map();
4115
+ const pendingDiagnosticNotifications = new Set();
4115
4116
  const getDiagnostics$1 = async (editor, resultPort) => {
4116
4117
  return streamDiagnosticProvider(editor, resultPort);
4117
4118
  };
@@ -4170,6 +4171,9 @@ const commitDiagnostics = async (editor, generation, diagnostics, onAccepted) =>
4170
4171
  }
4171
4172
  if (diagnosticsEqual(latest.newState.diagnostics, diagnostics)) {
4172
4173
  onAccepted?.();
4174
+ if (pendingDiagnosticNotifications.delete(editor.id)) {
4175
+ await notifyDiagnosticsChange(latest.newState.uri, latest.newState.applicationId);
4176
+ }
4173
4177
  return latest.newState;
4174
4178
  }
4175
4179
  let calculationState = latest.newState;
@@ -4191,6 +4195,7 @@ const commitDiagnostics = async (editor, generation, diagnostics, onAccepted) =>
4191
4195
  requestDiagnosticSnapshots.set(editor.id, diagnostics);
4192
4196
  onAccepted?.();
4193
4197
  await invoke$8('Editor.renderPending', newEditor.id);
4198
+ pendingDiagnosticNotifications.delete(editor.id);
4194
4199
  await notifyDiagnosticsChange(newEditor.uri, newEditor.applicationId);
4195
4200
  return newEditor;
4196
4201
  })) ?? editor;
@@ -4359,6 +4364,7 @@ const clearDiagnosticRequestState = editorId => {
4359
4364
  requestGenerations.delete(editorId);
4360
4365
  requestDiagnosticSnapshots.delete(editorId);
4361
4366
  previousProviderResults.delete(editorId);
4367
+ pendingDiagnosticNotifications.delete(editorId);
4362
4368
  };
4363
4369
  const transformCachedDiagnostics = (editorId, changes) => {
4364
4370
  const previous = previousProviderResults.get(editorId);
@@ -4367,9 +4373,13 @@ const transformCachedDiagnostics = (editorId, changes) => {
4367
4373
  }
4368
4374
  const providers = new Map();
4369
4375
  for (const [providerId, result] of previous.providers) {
4376
+ const diagnostics = transformDiagnostics(result.diagnostics, changes);
4377
+ if (!diagnosticsEqual(result.diagnostics, diagnostics)) {
4378
+ pendingDiagnosticNotifications.add(editorId);
4379
+ }
4370
4380
  providers.set(providerId, {
4371
4381
  ...result,
4372
- diagnostics: transformDiagnostics(result.diagnostics, changes)
4382
+ diagnostics
4373
4383
  });
4374
4384
  }
4375
4385
  previousProviderResults.set(editorId, {
@@ -6352,6 +6362,7 @@ const createEditor = async ({
6352
6362
  fontFamily,
6353
6363
  fontSize,
6354
6364
  fontWeight,
6365
+ formatActions,
6355
6366
  formatOnSave,
6356
6367
  height,
6357
6368
  highlightActiveLineNumber = true,
@@ -6416,6 +6427,7 @@ const createEditor = async ({
6416
6427
  fontFamily,
6417
6428
  fontSize,
6418
6429
  fontWeight,
6430
+ formatActions,
6419
6431
  formatOnSave,
6420
6432
  gutterDecorations: [],
6421
6433
  gutterWidth: 0,
@@ -7989,7 +8001,7 @@ const applyEdit = async (editor, changes, selectionChanges) => {
7989
8001
  return scheduleDocumentAndCursorsSelections(editor, changes, selectionChanges);
7990
8002
  };
7991
8003
 
7992
- const getFormattingEdits = async editor => {
8004
+ const getFormattingEdits$1 = async editor => {
7993
8005
  if (editor.largeFile) {
7994
8006
  return [];
7995
8007
  }
@@ -8265,26 +8277,32 @@ const showErrorMessage = async (editor, rowIndex, columnIndex, message) => {
8265
8277
  };
8266
8278
 
8267
8279
  const expectedErrorMessage = 'Failed to execute formatting provider: FormattingError:';
8268
-
8269
- // TODO also format with cursor
8270
- const format = async editor => {
8280
+ const getFormattingEdits = async editor => {
8271
8281
  try {
8272
- const edits = await getFormattingEdits(editor);
8273
- return applyDocumentEdits$1(editor, edits);
8282
+ return await getFormattingEdits$1(editor);
8274
8283
  } catch (error) {
8275
8284
  if (isFormattingError(error)) {
8276
8285
  console.error('Formatting Error:',
8277
8286
  // @ts-ignore
8278
8287
  error.message.slice(expectedErrorMessage.length));
8279
- return editor;
8288
+ return undefined;
8280
8289
  }
8281
8290
  console.error(error);
8282
8291
 
8283
8292
  // TODO configure editor message as widget
8284
8293
  const displayErrorMessage = String(error);
8285
8294
  await editorShowMessage(editor, 0, 0, displayErrorMessage);
8295
+ return undefined;
8296
+ }
8297
+ };
8298
+
8299
+ // TODO also format with cursor
8300
+ const format = async editor => {
8301
+ const edits = await getFormattingEdits(editor);
8302
+ if (!edits) {
8286
8303
  return editor;
8287
8304
  }
8305
+ return applyDocumentEdits$1(editor, edits);
8288
8306
  };
8289
8307
 
8290
8308
  const getNewEditor = async (editor, skipFormatting = false) => {
@@ -8363,29 +8381,41 @@ const save$1 = async (editor, skipFormatting = false) => {
8363
8381
  if (!isUntitledFile(uri) && (await isReadonlyFile(uri, applicationId))) {
8364
8382
  return editor;
8365
8383
  }
8366
- const newEditor = await getNewEditor(editor, skipFormatting);
8367
- const content = applyLineEndings(getText$1(newEditor), newEditor.endOfLine);
8384
+ const shouldFormat = editor.formatOnSave && !skipFormatting;
8385
+ const formatBeforeSave = shouldFormat && editor.formatActions === 'pre-save';
8386
+ const editorToSave = formatBeforeSave ? await getNewEditor(editor) : editor;
8387
+ const content = applyLineEndings(getText$1(editorToSave), editorToSave.endOfLine);
8368
8388
  if (isUntitledFile(uri)) {
8369
8389
  const pickedFilePath = await saveUntitledFile(uri, content, platform, applicationId);
8370
8390
  if (pickedFilePath) {
8371
- if (newEditor.modified) {
8391
+ const savedEditor = {
8392
+ ...editorToSave,
8393
+ uri: pickedFilePath
8394
+ };
8395
+ if (editor.modified || savedEditor.modified) {
8372
8396
  await notifyTabModifiedStatusChange(uri, false, applicationId);
8373
8397
  }
8374
8398
  return {
8375
- ...newEditor,
8399
+ ...savedEditor,
8376
8400
  modified: false,
8377
- uri: pickedFilePath
8401
+ uri: pickedFilePath,
8402
+ ...(shouldFormat && !formatBeforeSave && {
8403
+ postSaveFormat: true
8404
+ })
8378
8405
  };
8379
8406
  }
8380
- return newEditor;
8407
+ return editorToSave;
8381
8408
  }
8382
8409
  await saveNormalFile(uri, content, applicationId);
8383
- if (newEditor.modified) {
8410
+ if (editor.modified || editorToSave.modified) {
8384
8411
  await notifyTabModifiedStatusChange(uri, false, applicationId);
8385
8412
  }
8386
8413
  return {
8387
- ...newEditor,
8388
- modified: false
8414
+ ...editorToSave,
8415
+ modified: false,
8416
+ ...(shouldFormat && !formatBeforeSave && {
8417
+ postSaveFormat: true
8418
+ })
8389
8419
  };
8390
8420
  } catch (error) {
8391
8421
  // @ts-ignore
@@ -8480,6 +8510,25 @@ const registerStateCommand = command => {
8480
8510
  };
8481
8511
  const isStateCommand = command => stateCommands?.has(command) ?? false;
8482
8512
  const cursorUndoLimit = 100;
8513
+ const formatAfterSave = async (uid, snapshot) => {
8514
+ const originalText = getText$1(snapshot);
8515
+ const edits = await getFormattingEdits(snapshot);
8516
+ if (!edits?.length) {
8517
+ return;
8518
+ }
8519
+ const applyFormatting = wrapCommand$1(async editor => {
8520
+ const staleReason = snapshot.lifecycle ? editor.lifecycle === snapshot.lifecycle ? editor.uri === snapshot.uri ? editor.applicationId === snapshot.applicationId ? getText$1(editor) === originalText ? '' : `text:${originalText.length}:${getText$1(editor).length}` : 'application' : 'uri' : 'lifecycle' : 'missing-snapshot-lifecycle';
8521
+ if (staleReason) {
8522
+ return editor;
8523
+ }
8524
+ const formattedEditor = await applyDocumentEdits$1(editor, edits);
8525
+ if (formattedEditor === editor || getText$1(formattedEditor) === originalText) {
8526
+ return editor;
8527
+ }
8528
+ return save$1(formattedEditor, true);
8529
+ });
8530
+ await applyFormatting(uid);
8531
+ };
8483
8532
  const selectionsEqual = (left, right) => {
8484
8533
  if (left === right) {
8485
8534
  return true;
@@ -8545,10 +8594,18 @@ const wrapCommand$1 = (fn, preservesTypingCoalescing = false, returnState = true
8545
8594
  uri
8546
8595
  } = state;
8547
8596
  const commandResult = await fn(state, ...args);
8597
+ const shouldFormatAfterSave = commandResult?.postSaveFormat === true;
8548
8598
  let newEditor = !preservesTypingCoalescing && commandResult.canCoalesceTyping ? {
8549
8599
  ...commandResult,
8550
8600
  canCoalesceTyping: false
8551
8601
  } : commandResult;
8602
+ if (shouldFormatAfterSave) {
8603
+ const {
8604
+ postSaveFormat: _postSaveFormat,
8605
+ ...stateWithoutPostSaveFormat
8606
+ } = newEditor;
8607
+ newEditor = stateWithoutPostSaveFormat;
8608
+ }
8552
8609
  if (lines !== newEditor.lines && newEditor.cursorUndoStack?.length) {
8553
8610
  newEditor = {
8554
8611
  ...newEditor,
@@ -8617,6 +8674,9 @@ const wrapCommand$1 = (fn, preservesTypingCoalescing = false, returnState = true
8617
8674
  }
8618
8675
  }
8619
8676
  }
8677
+ if (shouldFormatAfterSave) {
8678
+ setTimeout(() => void formatAfterSave(uid, finalEditor), 0);
8679
+ }
8620
8680
  if (lines !== finalEditor.lines && !isUntitledFile(finalEditor.uri)) {
8621
8681
  schedule$1(uid, token => saveAfterDelay(uid, token));
8622
8682
  } else if (modified && !finalEditor.modified) {
@@ -16937,6 +16997,9 @@ const DEFAULT_HORIZONTAL_VIRTUALIZATION_THRESHOLD = 500;
16937
16997
  const getSelectedTextOccurrenceMatching = value => {
16938
16998
  return value === 'caseInsensitive' ? value : DEFAULT_SELECTED_TEXT_OCCURRENCE_MATCHING;
16939
16999
  };
17000
+ const getFormatActions = value => {
17001
+ return value === 'pre-save' ? 'pre-save' : 'post-save';
17002
+ };
16940
17003
  const getHoverDelay = value => {
16941
17004
  const delay = typeof value === 'number' ? value : Number(value);
16942
17005
  if (!Number.isFinite(delay) || delay < 0) {
@@ -16959,7 +17022,7 @@ const getHorizontalVirtualizationThreshold = value => {
16959
17022
  return Math.floor(threshold);
16960
17023
  };
16961
17024
  const getEditorPreferences = async applicationId => {
16962
- const [diagnosticsEnabled, fontFamily, fontSize, fontWeight, formatOnSave, hoverEnabled, hoverDelay, horizontalVirtualizationThreshold, messageDelay, isAutoClosingBracketsEnabled$1, isAutoClosingQuotesEnabled$1, isAutoClosingTagsEnabled$1, isQuickSuggestionsEnabled$1, lineNumbers, highlightActiveLineNumber, rowHeight, tabSize, letterSpacing, completionTriggerCharacters, minimapEnabled, mergeConflictActionsEnabled, breadcrumbsEnabled, insertSpaces, dragAndDropEnabled, roundedSelection, combineWhitespaceTokens, selectedTextOccurrenceMatching] = await Promise.all([get$3('editor.diagnostics', applicationId), getFontFamily(applicationId), getFontSize(applicationId), getFontWeight(applicationId), get$3('editor.formatOnSave', applicationId), get$3('editor.hover', applicationId), get$3('editor.hoverDelay', applicationId), get$3('editor.horizontalVirtualizationThreshold', applicationId), get$3('editor.messageDelay', applicationId), isAutoClosingBracketsEnabled(applicationId), isAutoClosingQuotesEnabled(applicationId), isAutoClosingTagsEnabled(), isQuickSuggestionsEnabled(applicationId), getLineNumbers(applicationId), getHighlightActiveLineNumber(applicationId), getRowHeight(applicationId), getTabSize(applicationId), getLetterSpacing(applicationId), getCompletionTriggerCharacters(), getMinimapEnabled(applicationId), getMergeConflictActionsEnabled(applicationId), get$3('breadcrumbs.enabled', applicationId), get$3('editor.insertSpaces', applicationId), get$3('editor.dragAndDrop', applicationId), get$3('editor.roundedSelection', applicationId), get$3('editor.combineWhitespaceTokens', applicationId), get$3('editor.selectedTextOccurrenceMatching', applicationId)]);
17025
+ const [diagnosticsEnabled, fontFamily, fontSize, fontWeight, formatOnSave, formatActions, hoverEnabled, hoverDelay, horizontalVirtualizationThreshold, messageDelay, isAutoClosingBracketsEnabled$1, isAutoClosingQuotesEnabled$1, isAutoClosingTagsEnabled$1, isQuickSuggestionsEnabled$1, lineNumbers, highlightActiveLineNumber, rowHeight, tabSize, letterSpacing, completionTriggerCharacters, minimapEnabled, mergeConflictActionsEnabled, breadcrumbsEnabled, insertSpaces, dragAndDropEnabled, roundedSelection, combineWhitespaceTokens, selectedTextOccurrenceMatching] = await Promise.all([get$3('editor.diagnostics', applicationId), getFontFamily(applicationId), getFontSize(applicationId), getFontWeight(applicationId), get$3('editor.formatOnSave', applicationId), get$3('editor.formatActions', applicationId), get$3('editor.hover', applicationId), get$3('editor.hoverDelay', applicationId), get$3('editor.horizontalVirtualizationThreshold', applicationId), get$3('editor.messageDelay', applicationId), isAutoClosingBracketsEnabled(applicationId), isAutoClosingQuotesEnabled(applicationId), isAutoClosingTagsEnabled(), isQuickSuggestionsEnabled(applicationId), getLineNumbers(applicationId), getHighlightActiveLineNumber(applicationId), getRowHeight(applicationId), getTabSize(applicationId), getLetterSpacing(applicationId), getCompletionTriggerCharacters(), getMinimapEnabled(applicationId), getMergeConflictActionsEnabled(applicationId), get$3('breadcrumbs.enabled', applicationId), get$3('editor.insertSpaces', applicationId), get$3('editor.dragAndDrop', applicationId), get$3('editor.roundedSelection', applicationId), get$3('editor.combineWhitespaceTokens', applicationId), get$3('editor.selectedTextOccurrenceMatching', applicationId)]);
16963
17026
  return {
16964
17027
  breadcrumbsEnabled: breadcrumbsEnabled ?? false,
16965
17028
  combineWhitespaceTokens: combineWhitespaceTokens ?? true,
@@ -16969,6 +17032,7 @@ const getEditorPreferences = async applicationId => {
16969
17032
  fontFamily,
16970
17033
  fontSize,
16971
17034
  fontWeight,
17035
+ formatActions: getFormatActions(formatActions),
16972
17036
  formatOnSave: formatOnSave ?? false,
16973
17037
  highlightActiveLineNumber,
16974
17038
  horizontalVirtualizationThreshold: getHorizontalVirtualizationThreshold(horizontalVirtualizationThreshold),
@@ -17052,6 +17116,7 @@ const handleSettingsChanged = async state => {
17052
17116
  charWidth,
17053
17117
  completionsOnType: !state.largeFile && Boolean(completionsOnTypeRaw),
17054
17118
  diagnostics: diagnosticsEnabled ? state.diagnostics : [],
17119
+ formatActions: editorPreferences.formatActions === 'pre-save' ? 'pre-save' : 'post-save',
17055
17120
  isMonospaceFont,
17056
17121
  itemHeight: rowHeight,
17057
17122
  minimapRevision: (state.minimapRevision || 0) + (minimapEnabled ? 1 : 0),
@@ -17513,6 +17578,7 @@ const loadContent = async (state, savedState, largeFile = false, focus = true, f
17513
17578
  fontFamily,
17514
17579
  fontSize,
17515
17580
  fontWeight,
17581
+ formatActions,
17516
17582
  formatOnSave,
17517
17583
  highlightActiveLineNumber,
17518
17584
  horizontalVirtualizationThreshold,
@@ -17552,6 +17618,7 @@ const loadContent = async (state, savedState, largeFile = false, focus = true, f
17552
17618
  fontFamily,
17553
17619
  fontSize,
17554
17620
  fontWeight,
17621
+ formatActions: formatActions === 'pre-save' ? 'pre-save' : 'post-save',
17555
17622
  formatOnSave,
17556
17623
  highlightActiveLineNumber,
17557
17624
  horizontalVirtualizationThreshold,
@@ -19415,15 +19482,24 @@ const revealProblem = (editor, rowIndex, columnIndex) => {
19415
19482
 
19416
19483
  const save = async (uid, skipFormatting = false) => {
19417
19484
  let savedFrom;
19485
+ let postSaveSnapshot;
19418
19486
  const saveContent = wrapCommand$1(async editor => {
19419
19487
  savedFrom = editor;
19420
- return save$1(editor, skipFormatting);
19488
+ const shouldFormatAfterSave = editor.formatOnSave && !skipFormatting && editor.formatActions !== 'pre-save';
19489
+ const savedEditor = await save$1(editor, skipFormatting || shouldFormatAfterSave);
19490
+ if (shouldFormatAfterSave && savedEditor !== editor) {
19491
+ postSaveSnapshot = savedEditor;
19492
+ }
19493
+ return savedEditor;
19421
19494
  });
19422
19495
  const saved = await saveContent(uid);
19423
19496
  if (savedFrom && saved && savedFrom.uri !== saved.uri) {
19424
19497
  // Retargeting calls Editor.handleUriChange, so release the editing queue first.
19425
19498
  await invoke$a(savedFrom.applicationId, 'Main.handleUriChange', savedFrom.uri, saved.uri);
19426
19499
  }
19500
+ if (saved && postSaveSnapshot) {
19501
+ setTimeout(() => void formatAfterSave(uid, saved), 0);
19502
+ }
19427
19503
  };
19428
19504
 
19429
19505
  const saveState = async (state, hotReload) => {
@@ -4525,6 +4525,7 @@ const mergeLinksWithDiagnosticDecorations = (editor, diagnosticDecorations) => {
4525
4525
  const requestGenerations = new Map();
4526
4526
  const requestDiagnosticSnapshots = new Map();
4527
4527
  const previousProviderResults = new Map();
4528
+ const pendingDiagnosticNotifications = new Set();
4528
4529
  const getDiagnostics$1 = async (editor, resultPort) => {
4529
4530
  return streamDiagnosticProvider(editor, resultPort);
4530
4531
  };
@@ -4583,6 +4584,9 @@ const commitDiagnostics = async (editor, generation, diagnostics, onAccepted) =>
4583
4584
  }
4584
4585
  if (diagnosticsEqual(latest.newState.diagnostics, diagnostics)) {
4585
4586
  onAccepted?.();
4587
+ if (pendingDiagnosticNotifications.delete(editor.id)) {
4588
+ await notifyDiagnosticsChange(latest.newState.uri, latest.newState.applicationId);
4589
+ }
4586
4590
  return latest.newState;
4587
4591
  }
4588
4592
  let calculationState = latest.newState;
@@ -4604,6 +4608,7 @@ const commitDiagnostics = async (editor, generation, diagnostics, onAccepted) =>
4604
4608
  requestDiagnosticSnapshots.set(editor.id, diagnostics);
4605
4609
  onAccepted?.();
4606
4610
  await invoke$8('Editor.renderPending', newEditor.id);
4611
+ pendingDiagnosticNotifications.delete(editor.id);
4607
4612
  await notifyDiagnosticsChange(newEditor.uri, newEditor.applicationId);
4608
4613
  return newEditor;
4609
4614
  })) ?? editor;
@@ -4772,6 +4777,7 @@ const clearDiagnosticRequestState = editorId => {
4772
4777
  requestGenerations.delete(editorId);
4773
4778
  requestDiagnosticSnapshots.delete(editorId);
4774
4779
  previousProviderResults.delete(editorId);
4780
+ pendingDiagnosticNotifications.delete(editorId);
4775
4781
  };
4776
4782
  const transformCachedDiagnostics = (editorId, changes) => {
4777
4783
  const previous = previousProviderResults.get(editorId);
@@ -4780,9 +4786,13 @@ const transformCachedDiagnostics = (editorId, changes) => {
4780
4786
  }
4781
4787
  const providers = new Map();
4782
4788
  for (const [providerId, result] of previous.providers) {
4789
+ const diagnostics = transformDiagnostics(result.diagnostics, changes);
4790
+ if (!diagnosticsEqual(result.diagnostics, diagnostics)) {
4791
+ pendingDiagnosticNotifications.add(editorId);
4792
+ }
4783
4793
  providers.set(providerId, {
4784
4794
  ...result,
4785
- diagnostics: transformDiagnostics(result.diagnostics, changes)
4795
+ diagnostics
4786
4796
  });
4787
4797
  }
4788
4798
  previousProviderResults.set(editorId, {
@@ -6765,6 +6775,7 @@ const createEditor = async ({
6765
6775
  fontFamily,
6766
6776
  fontSize,
6767
6777
  fontWeight,
6778
+ formatActions,
6768
6779
  formatOnSave,
6769
6780
  height,
6770
6781
  highlightActiveLineNumber = true,
@@ -6829,6 +6840,7 @@ const createEditor = async ({
6829
6840
  fontFamily,
6830
6841
  fontSize,
6831
6842
  fontWeight,
6843
+ formatActions,
6832
6844
  formatOnSave,
6833
6845
  gutterDecorations: [],
6834
6846
  gutterWidth: 0,
@@ -8402,7 +8414,7 @@ const applyEdit = async (editor, changes, selectionChanges) => {
8402
8414
  return scheduleDocumentAndCursorsSelections(editor, changes, selectionChanges);
8403
8415
  };
8404
8416
 
8405
- const getFormattingEdits = async editor => {
8417
+ const getFormattingEdits$1 = async editor => {
8406
8418
  if (editor.largeFile) {
8407
8419
  return [];
8408
8420
  }
@@ -8678,26 +8690,32 @@ const showErrorMessage = async (editor, rowIndex, columnIndex, message) => {
8678
8690
  };
8679
8691
 
8680
8692
  const expectedErrorMessage = 'Failed to execute formatting provider: FormattingError:';
8681
-
8682
- // TODO also format with cursor
8683
- const format = async editor => {
8693
+ const getFormattingEdits = async editor => {
8684
8694
  try {
8685
- const edits = await getFormattingEdits(editor);
8686
- return applyDocumentEdits$1(editor, edits);
8695
+ return await getFormattingEdits$1(editor);
8687
8696
  } catch (error) {
8688
8697
  if (isFormattingError(error)) {
8689
8698
  console.error('Formatting Error:',
8690
8699
  // @ts-ignore
8691
8700
  error.message.slice(expectedErrorMessage.length));
8692
- return editor;
8701
+ return undefined;
8693
8702
  }
8694
8703
  console.error(error);
8695
8704
 
8696
8705
  // TODO configure editor message as widget
8697
8706
  const displayErrorMessage = String(error);
8698
8707
  await editorShowMessage(editor, 0, 0, displayErrorMessage);
8708
+ return undefined;
8709
+ }
8710
+ };
8711
+
8712
+ // TODO also format with cursor
8713
+ const format = async editor => {
8714
+ const edits = await getFormattingEdits(editor);
8715
+ if (!edits) {
8699
8716
  return editor;
8700
8717
  }
8718
+ return applyDocumentEdits$1(editor, edits);
8701
8719
  };
8702
8720
 
8703
8721
  const getNewEditor = async (editor, skipFormatting = false) => {
@@ -8776,29 +8794,41 @@ const save$1 = async (editor, skipFormatting = false) => {
8776
8794
  if (!isUntitledFile(uri) && (await isReadonlyFile(uri, applicationId))) {
8777
8795
  return editor;
8778
8796
  }
8779
- const newEditor = await getNewEditor(editor, skipFormatting);
8780
- const content = applyLineEndings(getText$1(newEditor), newEditor.endOfLine);
8797
+ const shouldFormat = editor.formatOnSave && !skipFormatting;
8798
+ const formatBeforeSave = shouldFormat && editor.formatActions === 'pre-save';
8799
+ const editorToSave = formatBeforeSave ? await getNewEditor(editor) : editor;
8800
+ const content = applyLineEndings(getText$1(editorToSave), editorToSave.endOfLine);
8781
8801
  if (isUntitledFile(uri)) {
8782
8802
  const pickedFilePath = await saveUntitledFile(uri, content, platform, applicationId);
8783
8803
  if (pickedFilePath) {
8784
- if (newEditor.modified) {
8804
+ const savedEditor = {
8805
+ ...editorToSave,
8806
+ uri: pickedFilePath
8807
+ };
8808
+ if (editor.modified || savedEditor.modified) {
8785
8809
  await notifyTabModifiedStatusChange(uri, false, applicationId);
8786
8810
  }
8787
8811
  return {
8788
- ...newEditor,
8812
+ ...savedEditor,
8789
8813
  modified: false,
8790
- uri: pickedFilePath
8814
+ uri: pickedFilePath,
8815
+ ...(shouldFormat && !formatBeforeSave && {
8816
+ postSaveFormat: true
8817
+ })
8791
8818
  };
8792
8819
  }
8793
- return newEditor;
8820
+ return editorToSave;
8794
8821
  }
8795
8822
  await saveNormalFile(uri, content, applicationId);
8796
- if (newEditor.modified) {
8823
+ if (editor.modified || editorToSave.modified) {
8797
8824
  await notifyTabModifiedStatusChange(uri, false, applicationId);
8798
8825
  }
8799
8826
  return {
8800
- ...newEditor,
8801
- modified: false
8827
+ ...editorToSave,
8828
+ modified: false,
8829
+ ...(shouldFormat && !formatBeforeSave && {
8830
+ postSaveFormat: true
8831
+ })
8802
8832
  };
8803
8833
  } catch (error) {
8804
8834
  // @ts-ignore
@@ -8888,6 +8918,25 @@ const schedule = editor => {
8888
8918
  };
8889
8919
 
8890
8920
  const cursorUndoLimit = 100;
8921
+ const formatAfterSave = async (uid, snapshot) => {
8922
+ const originalText = getText$1(snapshot);
8923
+ const edits = await getFormattingEdits(snapshot);
8924
+ if (!edits?.length) {
8925
+ return;
8926
+ }
8927
+ const applyFormatting = wrapCommand$1(async editor => {
8928
+ const staleReason = snapshot.lifecycle ? editor.lifecycle === snapshot.lifecycle ? editor.uri === snapshot.uri ? editor.applicationId === snapshot.applicationId ? getText$1(editor) === originalText ? '' : `text:${originalText.length}:${getText$1(editor).length}` : 'application' : 'uri' : 'lifecycle' : 'missing-snapshot-lifecycle';
8929
+ if (staleReason) {
8930
+ return editor;
8931
+ }
8932
+ const formattedEditor = await applyDocumentEdits$1(editor, edits);
8933
+ if (formattedEditor === editor || getText$1(formattedEditor) === originalText) {
8934
+ return editor;
8935
+ }
8936
+ return save$1(formattedEditor, true);
8937
+ });
8938
+ await applyFormatting(uid);
8939
+ };
8891
8940
  const selectionsEqual = (left, right) => {
8892
8941
  if (left === right) {
8893
8942
  return true;
@@ -8953,10 +9002,18 @@ const wrapCommand$1 = (fn, preservesTypingCoalescing = false, returnState = true
8953
9002
  uri
8954
9003
  } = state;
8955
9004
  const commandResult = await fn(state, ...args);
9005
+ const shouldFormatAfterSave = commandResult?.postSaveFormat === true;
8956
9006
  let newEditor = !preservesTypingCoalescing && commandResult.canCoalesceTyping ? {
8957
9007
  ...commandResult,
8958
9008
  canCoalesceTyping: false
8959
9009
  } : commandResult;
9010
+ if (shouldFormatAfterSave) {
9011
+ const {
9012
+ postSaveFormat: _postSaveFormat,
9013
+ ...stateWithoutPostSaveFormat
9014
+ } = newEditor;
9015
+ newEditor = stateWithoutPostSaveFormat;
9016
+ }
8960
9017
  if (lines !== newEditor.lines && newEditor.cursorUndoStack?.length) {
8961
9018
  newEditor = {
8962
9019
  ...newEditor,
@@ -9025,6 +9082,9 @@ const wrapCommand$1 = (fn, preservesTypingCoalescing = false, returnState = true
9025
9082
  }
9026
9083
  }
9027
9084
  }
9085
+ if (shouldFormatAfterSave) {
9086
+ setTimeout(() => void formatAfterSave(uid, finalEditor), 0);
9087
+ }
9028
9088
  if (lines !== finalEditor.lines && !isUntitledFile(finalEditor.uri)) {
9029
9089
  schedule$1(uid, token => saveAfterDelay(uid, token));
9030
9090
  } else if (modified && !finalEditor.modified) {
@@ -17341,6 +17401,9 @@ const DEFAULT_HORIZONTAL_VIRTUALIZATION_THRESHOLD = 500;
17341
17401
  const getSelectedTextOccurrenceMatching = value => {
17342
17402
  return value === 'caseInsensitive' ? value : DEFAULT_SELECTED_TEXT_OCCURRENCE_MATCHING;
17343
17403
  };
17404
+ const getFormatActions = value => {
17405
+ return value === 'pre-save' ? 'pre-save' : 'post-save';
17406
+ };
17344
17407
  const getHoverDelay = value => {
17345
17408
  const delay = typeof value === 'number' ? value : Number(value);
17346
17409
  if (!Number.isFinite(delay) || delay < 0) {
@@ -17363,7 +17426,7 @@ const getHorizontalVirtualizationThreshold = value => {
17363
17426
  return Math.floor(threshold);
17364
17427
  };
17365
17428
  const getEditorPreferences = async applicationId => {
17366
- const [diagnosticsEnabled, fontFamily, fontSize, fontWeight, formatOnSave, hoverEnabled, hoverDelay, horizontalVirtualizationThreshold, messageDelay, isAutoClosingBracketsEnabled$1, isAutoClosingQuotesEnabled$1, isAutoClosingTagsEnabled$1, isQuickSuggestionsEnabled$1, lineNumbers, highlightActiveLineNumber, rowHeight, tabSize, letterSpacing, completionTriggerCharacters, minimapEnabled, mergeConflictActionsEnabled, breadcrumbsEnabled, insertSpaces, dragAndDropEnabled, roundedSelection, combineWhitespaceTokens, selectedTextOccurrenceMatching] = await Promise.all([get$3('editor.diagnostics', applicationId), getFontFamily(applicationId), getFontSize(applicationId), getFontWeight(applicationId), get$3('editor.formatOnSave', applicationId), get$3('editor.hover', applicationId), get$3('editor.hoverDelay', applicationId), get$3('editor.horizontalVirtualizationThreshold', applicationId), get$3('editor.messageDelay', applicationId), isAutoClosingBracketsEnabled(applicationId), isAutoClosingQuotesEnabled(applicationId), isAutoClosingTagsEnabled(), isQuickSuggestionsEnabled(applicationId), getLineNumbers(applicationId), getHighlightActiveLineNumber(applicationId), getRowHeight(applicationId), getTabSize(applicationId), getLetterSpacing(applicationId), getCompletionTriggerCharacters(), getMinimapEnabled(applicationId), getMergeConflictActionsEnabled(applicationId), get$3('breadcrumbs.enabled', applicationId), get$3('editor.insertSpaces', applicationId), get$3('editor.dragAndDrop', applicationId), get$3('editor.roundedSelection', applicationId), get$3('editor.combineWhitespaceTokens', applicationId), get$3('editor.selectedTextOccurrenceMatching', applicationId)]);
17429
+ const [diagnosticsEnabled, fontFamily, fontSize, fontWeight, formatOnSave, formatActions, hoverEnabled, hoverDelay, horizontalVirtualizationThreshold, messageDelay, isAutoClosingBracketsEnabled$1, isAutoClosingQuotesEnabled$1, isAutoClosingTagsEnabled$1, isQuickSuggestionsEnabled$1, lineNumbers, highlightActiveLineNumber, rowHeight, tabSize, letterSpacing, completionTriggerCharacters, minimapEnabled, mergeConflictActionsEnabled, breadcrumbsEnabled, insertSpaces, dragAndDropEnabled, roundedSelection, combineWhitespaceTokens, selectedTextOccurrenceMatching] = await Promise.all([get$3('editor.diagnostics', applicationId), getFontFamily(applicationId), getFontSize(applicationId), getFontWeight(applicationId), get$3('editor.formatOnSave', applicationId), get$3('editor.formatActions', applicationId), get$3('editor.hover', applicationId), get$3('editor.hoverDelay', applicationId), get$3('editor.horizontalVirtualizationThreshold', applicationId), get$3('editor.messageDelay', applicationId), isAutoClosingBracketsEnabled(applicationId), isAutoClosingQuotesEnabled(applicationId), isAutoClosingTagsEnabled(), isQuickSuggestionsEnabled(applicationId), getLineNumbers(applicationId), getHighlightActiveLineNumber(applicationId), getRowHeight(applicationId), getTabSize(applicationId), getLetterSpacing(applicationId), getCompletionTriggerCharacters(), getMinimapEnabled(applicationId), getMergeConflictActionsEnabled(applicationId), get$3('breadcrumbs.enabled', applicationId), get$3('editor.insertSpaces', applicationId), get$3('editor.dragAndDrop', applicationId), get$3('editor.roundedSelection', applicationId), get$3('editor.combineWhitespaceTokens', applicationId), get$3('editor.selectedTextOccurrenceMatching', applicationId)]);
17367
17430
  return {
17368
17431
  breadcrumbsEnabled: breadcrumbsEnabled ?? false,
17369
17432
  combineWhitespaceTokens: combineWhitespaceTokens ?? true,
@@ -17373,6 +17436,7 @@ const getEditorPreferences = async applicationId => {
17373
17436
  fontFamily,
17374
17437
  fontSize,
17375
17438
  fontWeight,
17439
+ formatActions: getFormatActions(formatActions),
17376
17440
  formatOnSave: formatOnSave ?? false,
17377
17441
  highlightActiveLineNumber,
17378
17442
  horizontalVirtualizationThreshold: getHorizontalVirtualizationThreshold(horizontalVirtualizationThreshold),
@@ -17456,6 +17520,7 @@ const handleSettingsChanged = async state => {
17456
17520
  charWidth,
17457
17521
  completionsOnType: !state.largeFile && Boolean(completionsOnTypeRaw),
17458
17522
  diagnostics: diagnosticsEnabled ? state.diagnostics : [],
17523
+ formatActions: editorPreferences.formatActions === 'pre-save' ? 'pre-save' : 'post-save',
17459
17524
  isMonospaceFont,
17460
17525
  itemHeight: rowHeight,
17461
17526
  minimapRevision: (state.minimapRevision || 0) + (minimapEnabled ? 1 : 0),
@@ -17917,6 +17982,7 @@ const loadContent = async (state, savedState, largeFile = false, focus = true, f
17917
17982
  fontFamily,
17918
17983
  fontSize,
17919
17984
  fontWeight,
17985
+ formatActions,
17920
17986
  formatOnSave,
17921
17987
  highlightActiveLineNumber,
17922
17988
  horizontalVirtualizationThreshold,
@@ -17956,6 +18022,7 @@ const loadContent = async (state, savedState, largeFile = false, focus = true, f
17956
18022
  fontFamily,
17957
18023
  fontSize,
17958
18024
  fontWeight,
18025
+ formatActions: formatActions === 'pre-save' ? 'pre-save' : 'post-save',
17959
18026
  formatOnSave,
17960
18027
  highlightActiveLineNumber,
17961
18028
  horizontalVirtualizationThreshold,
@@ -19819,15 +19886,24 @@ const revealProblem = (editor, rowIndex, columnIndex) => {
19819
19886
 
19820
19887
  const save = async (uid, skipFormatting = false) => {
19821
19888
  let savedFrom;
19889
+ let postSaveSnapshot;
19822
19890
  const saveContent = wrapCommand$1(async editor => {
19823
19891
  savedFrom = editor;
19824
- return save$1(editor, skipFormatting);
19892
+ const shouldFormatAfterSave = editor.formatOnSave && !skipFormatting && editor.formatActions !== 'pre-save';
19893
+ const savedEditor = await save$1(editor, skipFormatting || shouldFormatAfterSave);
19894
+ if (shouldFormatAfterSave && savedEditor !== editor) {
19895
+ postSaveSnapshot = savedEditor;
19896
+ }
19897
+ return savedEditor;
19825
19898
  });
19826
19899
  const saved = await saveContent(uid);
19827
19900
  if (savedFrom && saved && savedFrom.uri !== saved.uri) {
19828
19901
  // Retargeting calls Editor.handleUriChange, so release the editing queue first.
19829
19902
  await invoke$a(savedFrom.applicationId, 'Main.handleUriChange', savedFrom.uri, saved.uri);
19830
19903
  }
19904
+ if (saved && postSaveSnapshot) {
19905
+ setTimeout(() => void formatAfterSave(uid, saved), 0);
19906
+ }
19831
19907
  };
19832
19908
 
19833
19909
  const saveState = async (state, hotReload) => {
@@ -43,6 +43,24 @@
43
43
  "type": "boolean",
44
44
  "value": false
45
45
  },
46
+ {
47
+ "category": "text-editor",
48
+ "description": "Controls when formatting actions run during save",
49
+ "heading": "Format Actions",
50
+ "id": "editor.formatActions",
51
+ "options": [
52
+ {
53
+ "id": "pre-save",
54
+ "label": "Before saving"
55
+ },
56
+ {
57
+ "id": "post-save",
58
+ "label": "After saving"
59
+ }
60
+ ],
61
+ "type": "enum",
62
+ "value": "post-save"
63
+ },
46
64
  {
47
65
  "category": "text-editor",
48
66
  "description": "Show completions automatically while typing",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "19.76.3",
3
+ "version": "19.77.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git@github.com:lvce-editor/editor-worker.git"