Provides snippets for the editor and manages drag-and-drop functionality. Implements vscode.TreeDataProvider and vscode.TreeDragAndDropController for snippet data and interaction.

Implements

Constructors

Properties

dragMimeTypes: readonly string[] = ...

The MIME types supported for drag operations in the snippets provider. This is used when snippets are dragged from the provider.

dropMimeTypes: readonly string[] = ...

The MIME types supported for drop operations in the snippets provider. This is used when other items are dropped onto the provider.

fn: () => void

A function that can be set externally to handle UI updates or events.

onDidChangeTreeData: Event<undefined | null | void | Snippet> = ...

Event that is fired when the tree data changes.

This event is used to trigger an update of the tree view in VSCode when the data changes, such as after adding, removing, or updating a snippet.

Methods

  • Adds a new snippet to the snippet service, optionally associating it with a parent and providing metadata such as a description and language extension.

    This method first determines the file extension from the snippet name and attempts to match it with a language configuration. If a matching language is found, it updates the language extension and removes the file extension from the snippet name. The snippet is then added to the snippet service with a unique ID, and the state is synchronized.

    Parameters

    • name: string

      The name of the snippet, which may include a file extension.

    • snippet: string

      The content of the snippet.

    • parentId: number

      The ID of the parent snippet or folder.

    • Optionaldescription: string

      An optional description for the snippet.

    • OptionallanguageExt: string

      An optional language extension to associate with the snippet.

    • OptionalgistId: string

      An optional Gist ID for the snippet, if it was shared through GitHub Gist.

    Returns void

  • Adds a new folder for snippets to the snippet service, associating it with a parent and optionally providing an icon.

    This method generates a new unique ID for the folder, then adds the folder to the snippet service with the given name, parent ID, and optional icon. The state is then synchronized to reflect the new folder.

    Parameters

    • name: string

      The name of the folder to be added.

    • parentId: number

      The ID of the parent snippet or folder to which this new folder will belong.

    • Optionalicon: string

      An optional icon to associate with the folder, which can be displayed in the UI.

    Returns number

    The unique ID assigned to the newly created folder.

  • Edits an existing snippet by updating its information in the snippet service and synchronizing the state.

    This method updates the snippet with the provided data and ensures that the state is refreshed.

    Parameters

    • snippet: Snippet

      The snippet to be updated.

    Returns void

  • Edits an existing snippet folder by updating its information in the snippet service and synchronizing the state.

    This method updates the snippet folder with the provided data and ensures that the state is refreshed.

    Parameters

    • snippet: Snippet

      The snippet folder to be updated.

    Returns void

  • Exports the snippets to a specified destination path.

    This method exports all snippets, starting from the root parent, to a given destination path. After exporting, it calls the sync() method to ensure the snippets' state is updated.

    Parameters

    • destinationPath: string

      The path where the snippets should be exported.

    Returns void

  • Fixes the last used snippet ID.

    This method ensures that the last ID used for a snippet is fixed by calling the fixLastId method on the snippet service.

    Returns void

  • Fixes corrupted and duplicate snippets within the system.

    This method performs the following operations:

    1. Fixes the last used snippet ID.
    2. Identifies and fixes corrupted snippets with duplicate IDs by overriding their IDs.
    3. Moves non-folder snippets (with children) into a newly created folder and fixes their parent-child relationships.
    4. Synchronizes the system state after fixing duplicates and corrupted snippets.

    Returns number[]

    An array containing two numbers: the count of fixed duplicate snippets and the count of corrupted snippets.

  • Returns the children of the given snippet, or the root-level snippets if no element is provided.

    This method is used by the TreeDataProvider to load the children of a snippet when expanding a node in the VSCode Tree view. If no snippet is provided, the root-level snippets will be returned.

    Parameters

    • Optionalelement: Snippet

      The snippet whose children should be loaded. If not provided, the root snippets will be returned.

    Returns Thenable<Snippet[]>

    A promise that resolves to an array of Snippet objects, representing the children of the provided snippet or the root snippets.

  • Returns the vscode.TreeItem representation of the given snippet.

    This method is used by the TreeDataProvider to convert a snippet into a tree item that can be displayed in the VSCode Tree view.

    Parameters

    • element: Snippet

      The snippet that should be converted into a tree item.

    Returns TreeItem

    The vscode.TreeItem representation of the snippet.

  • Handles the drag event and sets the necessary MIME type data on the dataTransfer object.

    Parameters

    • source: readonly Snippet[]

      The list of Snippet objects being dragged.

    • dataTransfer: DataTransfer

      The vscode.DataTransfer object that holds the data being transferred during the drag.

    • token: CancellationToken

      A vscode.CancellationToken to signal when the operation should be canceled.

    Returns void | Thenable<void>

    A promise that resolves when the drag operation is complete, or void if no additional action is needed.

  • Handles the drop event for the snippets provider, managing how snippets are moved within the tree structure.

    This method is invoked when a user drops a snippet onto another snippet or folder. It validates the drop action, handles moving snippets, and ensures that folder hierarchy constraints are respected.

    Parameters

    • target: undefined | Snippet

      The target snippet or folder where the snippet is being dropped. If undefined, the root of the tree is considered.

    • dataTransfer: DataTransfer

      The vscode.DataTransfer object containing the dragged data (snippets).

    • token: CancellationToken

      A vscode.CancellationToken to cancel the operation if needed.

    Returns void | Thenable<void>

    A promise that resolves when the drop operation is completed or void if no action is taken.

  • Imports snippets from a specified source path.

    This method imports snippets from a given destination path and returns whether the import was successful. It checks if the root element has children after the import, returning true if there are children. After importing, it calls the sync() method to update the snippets' state.

    Parameters

    • destinationPath: string

      The path from which to import snippets.

    Returns boolean

    A boolean indicating whether the import was successful (i.e., if the root element has children after import).

  • Moves a snippet down in the list of snippets by adjusting its position and synchronizing the state.

    This method changes the position of the snippet by one step downwards and ensures that the state is updated.

    Parameters

    • snippet: Snippet

      The snippet to be moved down.

    Returns void

  • Moves a snippet up in the list of snippets by adjusting its position and synchronizing the state.

    This method changes the position of the snippet by one step upwards and ensures that the state is updated.

    Parameters

    • snippet: Snippet

      The snippet to be moved up.

    Returns void

  • Refreshes the tree data by calling the refresh method of the snippet service and firing the change event.

    This method updates the tree view to reflect any changes that may have occurred in the snippet data. It is typically called when data in the snippet service is updated or refreshed.

    Returns void

  • Removes a snippet from the snippet service and synchronizes the state.

    This method deletes the snippet and updates the state to reflect the removal.

    Parameters

    • snippet: Snippet

      The snippet to be removed.

    Returns void

  • Sets a function to handle UI updates or events, typically passed from other parts of the extension.

    Parameters

    • fn: () => void

      The function to set for handling UI updates or events.

    Returns void

  • Sorts all snippets across all folders or categories and synchronizes the state.

    This method organizes all snippets globally within the application and updates the state accordingly.

    Returns void

  • Sorts the snippets in a specific folder or category and synchronizes the state.

    This method organizes the snippets in a desired order within their category and ensures the state is refreshed.

    Parameters

    • snippet: Snippet

      The folder or category containing the snippets to be sorted.

    Returns void

  • Saves the current state of snippets and then refreshes the tree data.

    This method first persists the current snippets state by saving it through the snippet service, then it refreshes the tree view by calling the refresh method. If a callback function is provided, it is executed after the sync operation.

    Returns void