vscode#TextDocumentWillSaveEvent TypeScript Examples

The following examples show how to use vscode#TextDocumentWillSaveEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: extension.ts    From format-imports-vscode with MIT License 6 votes vote down vote up
function sortImportsBeforeSavingDocument(event: TextDocumentWillSaveEvent) {
  const { document } = event;
  const format = async () => {
    const newSourceText = await formatDocument(document, 'onSave');
    if (newSourceText === undefined) return [];
    return [TextEdit.replace(fullRange(document), newSourceText)];
  };
  event.waitUntil(format());
}
Example #2
Source File: extension.ts    From utopia with MIT License 5 votes vote down vote up
function willSaveText(path: string, event: TextDocumentWillSaveEvent): WillSaveText {
  return {
    type: 'WILL_SAVE_TEXT',
    path: path,
    event: event,
  }
}
Example #3
Source File: WorkspaceWatcher.ts    From dendron with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
   * If note is in workspace, execute {@link onWillSaveNote}
   * @param event
   * @returns
   */
  onWillSaveTextDocument(event: TextDocumentWillSaveEvent) {
    try {
      const ctx = "WorkspaceWatcher:onWillSaveTextDocument";
      const uri = event.document.uri;
      Logger.info({
        ctx,
        url: uri.fsPath,
        reason: TextDocumentSaveReason[event.reason],
        msg: "enter",
      });
      const { wsRoot, vaults } = this._extension.getDWorkspace();
      if (
        !WorkspaceUtils.isPathInWorkspace({ fpath: uri.fsPath, wsRoot, vaults })
      ) {
        Logger.debug({
          ctx,
          uri: uri.fsPath,
          msg: "not in workspace, ignoring.",
        });
        return { changes: [] };
      }

      if (uri.fsPath.endsWith(".md")) {
        return this.onWillSaveNote(event);
      } else {
        Logger.debug({
          ctx,
          uri: uri.fsPath,
          msg: "File type is not registered for updates. ignoring.",
        });
        return { changes: [] };
      }
    } catch (error) {
      Sentry.captureException(error);
      throw error;
    }
  }
Example #4
Source File: WorkspaceWatcher.ts    From dendron with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
   * When saving a note, do some book keeping
   * - update the `updated` time in frontmatter
   * - update the note metadata in the engine
   * @param event
   * @returns
   */
  private onWillSaveNote(event: TextDocumentWillSaveEvent) {
    const ctx = "WorkspaceWatcher:onWillSaveNote";
    const uri = event.document.uri;
    const engine = this._extension.getEngine();
    const fname = path.basename(uri.fsPath, ".md");
    const now = Time.now().toMillis();

    const note = NoteUtils.getNoteByFnameFromEngine({
      fname,
      vault: this._extension.wsUtils.getVaultFromUri(uri),
      engine,
    });

    // If we can't find the note, don't do anything
    if (!note) {
      // Log at info level and not error level for now to reduce Sentry noise
      Logger.info({
        ctx,
        msg: `Note with fname ${fname} not found in engine! Skipping updated field FM modification.`,
      });
      return;
    }

    // Return undefined if document is missing frontmatter
    if (!TextDocumentService.containsFrontmatter(event.document)) {
      return;
    }

    const content = event.document.getText();
    const match = NoteUtils.RE_FM_UPDATED.exec(content);
    let changes: TextEdit[] = [];

    // update the `updated` time in frontmatter if it exists and content has changed
    if (match && WorkspaceUtils.noteContentChanged({ content, note })) {
      Logger.info({ ctx, match, msg: "update activeText editor" });
      const startPos = event.document.positionAt(match.index);
      const endPos = event.document.positionAt(match.index + match[0].length);
      changes = [
        TextEdit.replace(new Range(startPos, endPos), `updated: ${now}`),
      ];

      // update the note in engine
      // eslint-disable-next-line  no-async-promise-executor
      const p = new Promise(async (resolve) => {
        note.updated = now;
        await engine.updateNote(note);
        return resolve(changes);
      });
      event.waitUntil(p);
    }
    return { changes };
  }