vscode#FileRenameEvent TypeScript Examples

The following examples show how to use vscode#FileRenameEvent. 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: WorkspaceWatcher.ts    From dendron with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * method to make modifications to the workspace after the file is renamed.
   * It updates the title of the note wrt the new fname and refreshes tree view
   */
  async onDidRenameFiles(args: FileRenameEvent) {
    // No-op if we're not in a Dendron Workspace
    if (!this._extension.isActive()) {
      return;
    }
    try {
      const files = args.files[0];
      const { newUri } = files;
      const fname = DNodeUtils.fname(newUri.fsPath);
      const engine = this._extension.getEngine();
      const { vaults, wsRoot } = this._extension.getDWorkspace();
      const newVault = VaultUtils.getVaultByFilePath({
        vaults,
        wsRoot,
        fsPath: newUri.fsPath,
      });
      const vpath = vault2Path({ wsRoot, vault: newVault });
      const newLocPath = path.join(vpath, fname + ".md");
      const noteRaw = file2Note(newLocPath, newVault);
      const newNote = NoteUtils.hydrate({
        noteRaw,
        noteHydrated: engine.notes[noteRaw.id],
      });
      newNote.title = NoteUtils.genTitle(fname);
      await engine.writeNote(newNote, { updateExisting: true });
    } catch (error: any) {
      Sentry.captureException(error);
      throw error;
    }
  }
Example #2
Source File: handlers.ts    From memo with MIT License 5 votes vote down vote up
handleFilesRename = async ({ files }: FileRenameEvent) => {
  await cache.cacheUris();

  if (!getMemoConfigProperty('links.sync.enabled', true)) {
    return;
  }

  if (files.some(({ newUri }) => fs.lstatSync(newUri.fsPath).isDirectory())) {
    window.showWarningMessage(
      'Recursive links update on directory rename is currently not supported.',
    );
  }

  let pathsUpdated: string[] = [];

  let refsUpdated: number = 0;

  const addToPathsUpdated = (path: string) =>
    (pathsUpdated = [...new Set([...pathsUpdated, path])]);

  const incrementRefsCounter = () => (refsUpdated += 1);

  const refsReplaceMap = await resolveRefsReplaceMap(files);

  for (const fsPath in refsReplaceMap) {
    const doc = await workspace.openTextDocument(Uri.file(fsPath));
    const refsReplaceEntry = refsReplaceMap[fsPath];

    const nextContent = replaceRefsInDoc(refsReplaceEntry, doc, {
      onMatch: () => addToPathsUpdated(fsPath),
      onReplace: incrementRefsCounter,
    });

    if (nextContent !== null) {
      fs.writeFileSync(fsPath, nextContent);
    }
  }

  if (pathsUpdated.length > 0) {
    window.showInformationMessage(
      `Updated ${refsUpdated} link${refsUpdated === 0 || refsUpdated === 1 ? '' : 's'} in ${
        pathsUpdated.length
      } file${pathsUpdated.length === 0 || pathsUpdated.length === 1 ? '' : 's'}`,
    );
  }
}