npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kareemkermad/monaco-auto-import

v1.1.6

Published

Easily add auto-import to the Monaco editor, with Javascript & Typescript support.

Downloads

16

Readme

monaco-auto-import

npm version npm downloads Demo

Forked from https://github.com/stackblitz/monaco-auto-import

Easily add auto-import to the Monaco editor, with Javascript & Typescript support.

Demo

Example code

import AutoImport, { regexTokeniser } from '@kareemkermad/monaco-auto-import'

const editor = monaco.editor.create(document.getElementById('demo'), {
  value: `
    PAD
    leftPad
    rightPad
  `,
  language: 'typescript'
})

const completor = new AutoImport({ monaco: monaco, editor: editor, spacesBetweenBraces: true, doubleQuotes: true, semiColon: true, alwaysApply: false });

completor.imports.saveFile({
  path: './node_modules/left-pad/index.js',
  aliases: ['left-pad'],
  imports: regexTokeniser(`
    export const PAD = ''
    export function leftPad() {}
    export function rightPad() {}
  `)
})

Getting started

Installing

yarn add @kareemkermad/monaco-auto-import
# or
npm i @kareemkermad/monaco-auto-import --save

How to Use

Initializing a new instance

Simply create a new Monaco editor instance and pass it to AutoImport. This will register custom completion providers for Monaco's javascript and typescript language services.

import AutoImport from '@kareemkermad/monaco-auto-import'

const editor = monaco.editor.create(document.getElementById('demo'), {
  language: 'typescript'
})

const completor = new AutoImport({ monaco: monaco, editor: editor, spacesBetweenBraces: true, doubleQuotes: true, semiColon: true, alwaysApply: false });

Options

  • monaco - Monaco instance.

  • editor - Monaco editor instance.

  • spacesBetweenBraces - True uses a space between the curly braces, false otherwise. e.g. import { Test } from "testing" or import {Test} from "testing"

  • doubleQuotes - True uses double quotes in the import path, false uses single quotes. e.g. import { Test } from "testing" or import {Test} from 'testing'

  • semiColon - True uses a semi colon after the import path, false otherwise. e.g. import { Test } from "testing"; or import {Test} from 'testing'

  • alwaysApply - True will always use the spacesBetweenBraces, doubleQuotes and semiColon options. False will only use them the first time you auto import and then automatically preserve any changes you make.

e.g. If alwaysApply = true and doubleQuotes = true then it will always use double quotes whenever you auto import. If alwaysApply = false and doubleQuotes = true then it will use double quotes the first time you auto import. But if you decide to modify the string to use single quotes then it will preserve the single quotes every other time you try to auto import.

Providing completion items

To make the auto-importer aware of a file with exports, simply call completor.imports.saveFile.

completor.imports.saveFile({
  path: './src/my-app.js',
  imports: [
    { type: 'const', name: 'Testing' },
    { type: 'class', name: 'World' },
    { type: 'class', name: 'type Universe' },
    { type: 'interface', name: 'Shape' },
    { type: 'function', name: 'drawShape' },
    { type: 'enum', name: 'Primitive' },
  ]
})

Tokenization

This package includes a built-in regexTokeniser, which uses a simple Regex to extracts exports from Javascript / Typescript code

import { regexTokeniser } from '@kareemkermad/monaco-auto-import'

const imports = regexTokeniser(`
  export const a = 1
  export class Test {}
  export interface type Shape
`)
// [{ type: 'const', name: 'a'}, { type: 'class', name: 'Test' }, { type: 'interface', name: 'type Shape' }]

completor.imports.saveFile({
  path: './src/my-app.js',
  imports: imports
})

New Features and Bug Fixes (https://github.com/stackblitz/monaco-auto-import)

  • Added monaco, editor, spacesBetweenBraces, doubleQuotes, semiColon, alwaysApply options.
  • Added support for import type - e.g. import {type Test} from "testing";
  • Added support for module.
  • Updated auto import suggestion to not show up a second time if it has already been imported.
  • Updated auto import suggestion description for import type.
  • Fixed import merging not working when swapping from single quotes to double quotes.
  • Fixed a bug that caused the name to autocomplete twice.
  • Fixed a bug that caused the mouse cursor position to move to a previous line.

API

imports.saveFile(file: File): void

Saves a file to the internal store, making it available for completion

imports.saveFiles(files: File[]): void

Bulk-saves files to the internal store from an Array of files

imports.getFile(path: string): File

Fetches a file from the internal store by it's path name (or one of it's aliases).

imports.getImports(name: string): ImportObject[]

Returns all the imports that exactly match a given string.

imports.addImport(path: string, name: string, type?: Expression): boolean

Adds an import to a given file, with an optional type paramater. Returns true if the file existed

imports.removeImport(path: string, name: string): boolean

Removes an import from a given file. Returns true if the file existed