redguardtoo / evil-matchit

Vim matchit ported into Emacs
GNU General Public License v3.0
287 stars 31 forks source link

evilmi broken in js ts rjsx mode #120

Closed yqrashawn closed 4 years ago

yqrashawn commented 4 years ago

evilmi-jump-items jump between } and < Screenshot-2020-05-28-15.56.52.mp4.zip

redguardtoo commented 4 years ago

What's you major mode?

yqrashawn commented 4 years ago

It happends in js-mode js2-mode rjsx-mode typescript-mode.

yqrashawn commented 4 years ago

It can be reproduced by below step.

use below file as ~/.emacs.d/init.debug.el, use the last code block as the ~/Download/test.js file.

run emacs -Q -l ~/.emacs.d/init.debug.el Note: this straight.el will create a ~/.emacs.d/straight folder and donwload some packages there.

;; ~/.emacs.d/init.debug.el
(setq load-prefer-newer t)
(when (fboundp 'tool-bar-mode)
  (tool-bar-mode -1))
(toggle-scroll-bar -1)
(menu-bar-mode -1)
(package-initialize)
(setq scroll-bar-background nil)

(setq gc-cons-threshold 100000000)

(defun my-minibuffer-setup-hook ()
  (setq gc-cons-threshold most-positive-fixnum))
(defun my-minibuffer-exit-hook ()
  (setq gc-cons-threshold (* 8 1024 1024)))
(add-hook 'minibuffer-setup-hook #'my-minibuffer-setup-hook)
(add-hook 'minibuffer-exit-hook #'my-minibuffer-exit-hook)

(let ((bootstrap-file (concat user-emacs-directory "straight/repos/straight.el/bootstrap.el"))
      (bootstrap-version 3))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(straight-use-package 'evil)
(straight-use-package 'evil-matchit)
(straight-use-package 'js2-mode)
(straight-use-package 'rjsx-mode)
(straight-use-package 'typescript-mode)

(find-file (expand-file-name "~/Downloads/test.js"))
(js2-mode)
(evil-mode)
(evil-matchit-mode)
// ~/Downloads/test.js
import pump from 'pump'
import querystring from 'querystring'
import LocalMessageDuplexStream from 'post-message-stream'
import ObjectMultiplex from 'obj-multiplex'
import extension from 'extensionizer'
import PortStream from 'extension-port-stream'

// These require calls need to use require to be statically recognized by browserify
const fs = require('fs')
const path = require('path')

const inpageContent = fs.readFileSync(
  path.join(__dirname, '..', '..', 'dist', 'chrome', 'inpage.js'),
  'utf8'
)
const inpageSuffix =
  '//# sourceURL=' + extension.runtime.getURL('inpage.js') + '\n'
const inpageBundle = inpageContent + inpageSuffix

// Eventually this streaming injection could be replaced with:
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.exportFunction
//
// But for now that is only Firefox
// If we create a FireFox-only code path using that API,
// MetaMask will be much faster loading and performant on Firefox.

if (shouldInjectProvider()) {
  injectScript(inpageBundle)
  start()
}

/**
 * Injects a script tag into the current document
 *
 * @param {string} content - Code to be executed in the current document
 */
function injectScript (content) {
  try {
    const container = document.head || document.documentElement
    const scriptTag = document.createElement('script')
    scriptTag.setAttribute('async', 'false')
    scriptTag.textContent = content
    container.insertBefore(scriptTag, container.children[0])
    container.removeChild(scriptTag)
  } catch (e) {
    console.error('MetaMask provider injection failed.', e)
  }
}

/**
 * Sets up the stream communication and submits site metadata
 *
 */
async function start () {
  await setupStreams()
  await domIsReady()
}

/**
 * Sets up two-way communication streams between the
 * browser extension and local per-page browser context.
 *
 */
async function setupStreams () {
  // the transport-specific streams for communication between inpage and background
  const pageStream = new LocalMessageDuplexStream({
    name: 'contentscript',
    target: 'inpage',
  })
  const extensionPort = extension.runtime.connect({ name: 'contentscript' })
  const extensionStream = new PortStream(extensionPort)

  // create and connect channel muxers
  // so we can handle the channels individually
  const pageMux = new ObjectMultiplex()
  pageMux.setMaxListeners(25)
  const extensionMux = new ObjectMultiplex()
  extensionMux.setMaxListeners(25)

  pump(pageMux, pageStream, pageMux, (err) =>
    logStreamDisconnectWarning('MetaMask Inpage Multiplex', err)
  )
  pump(extensionMux, extensionStream, extensionMux, (err) =>
    logStreamDisconnectWarning('MetaMask Background Multiplex', err)
  )

  // forward communication across inpage-background for these channels only
  forwardTrafficBetweenMuxers('confluxPortalProvider', pageMux, extensionMux)
  forwardTrafficBetweenMuxers(
    'confluxPortalPublicConfig',
    pageMux,
    extensionMux
  )

  // connect "phishing" channel to warning system
  const phishingStream = extensionMux.createStream('confluxPortalPhishing')
  phishingStream.once('data', redirectToPhishingWarning)
}

function forwardTrafficBetweenMuxers (channelName, muxA, muxB) {
  const channelA = muxA.createStream(channelName)
  const channelB = muxB.createStream(channelName)
  pump(channelA, channelB, channelA, (err) =>
    logStreamDisconnectWarning(
      `MetaMask muxed traffic for channel "${channelName}" failed.`,
      err
    )
  )
}

/**
 * Error handler for page to extension stream disconnections
 *
 * @param {string} remoteLabel - Remote stream name
 * @param {Error} err - Stream connection error
 */
function logStreamDisconnectWarning (remoteLabel, err) {
  let warningMsg = `MetamaskContentscript - lost connection to ${remoteLabel}`
  if (err) {
    warningMsg += '\n' + err.stack
  }
  console.warn(warningMsg)
}

/**
 * Determines if the provider should be injected
 *
 * @returns {boolean} {@code true} - if the provider should be injected
 */
function shouldInjectProvider () {
  return (
    doctypeCheck() &&
    suffixCheck() &&
    documentElementCheck() &&
    !blacklistedDomainCheck()
  )
}

/**
 * Checks the doctype of the current document if it exists
 *
 * @returns {boolean} {@code true} - if the doctype is html or if none exists
 */
function doctypeCheck () {
  const doctype = window.document.doctype
  if (doctype) {
    return doctype.name === 'html'
  } else {
    return true
  }
}

/**
 * Returns whether or not the extension (suffix) of the current document is prohibited
 *
 * This checks {@code window.location.pathname} against a set of file extensions
 * that we should not inject the provider into. This check is indifferent of
 * query parameters in the location.
 *
 * @returns {boolean} - whether or not the extension of the current document is prohibited
 */
function suffixCheck () {
  const prohibitedTypes = [/\.xml$/, /\.pdf$/]
  const currentUrl = window.location.pathname
  for (let i = 0; i < prohibitedTypes.length; i++) {
    if (prohibitedTypes[i].test(currentUrl)) {
      return false
    }
  }
  return true
}

/**
 * Checks the documentElement of the current document
 *
 * @returns {boolean} {@code true} - if the documentElement is an html node or if none exists
 */
function documentElementCheck () {
  const documentElement = document.documentElement.nodeName
  if (documentElement) {
    return documentElement.toLowerCase() === 'html'
  }
  return true
}

/**
 * Checks if the current domain is blacklisted
 *
 * @returns {boolean} {@code true} - if the current domain is blacklisted
 */
function blacklistedDomainCheck () {
  const blacklistedDomains = [
    'uscourts.gov',
    'dropbox.com',
    'webbyawards.com',
    'cdn.shopify.com/s/javascripts/tricorder/xtld-read-only-frame.html',
    'adyen.com',
    'gravityforms.com',
    'harbourair.com',
    'ani.gamer.com.tw',
    'blueskybooking.com',
    'sharefile.com',
  ]
  const currentUrl = window.location.href
  let currentRegex
  for (let i = 0; i < blacklistedDomains.length; i++) {
    const blacklistedDomain = blacklistedDomains[i].replace('.', '\\.')
    currentRegex = new RegExp(
      `(?:https?:\\/\\/)(?:(?!${blacklistedDomain}).)*$`
    )
    if (!currentRegex.test(currentUrl)) {
      return true
    }
  }
  return false
}

/**
 * Redirects the current page to a phishing information page
 */
function redirectToPhishingWarning () {
  console.log('MetaMask - routing to Phishing Warning component')
  const extensionURL = extension.runtime.getURL('phishing.html')
  window.location.href = `${extensionURL}#${querystring.stringify({
    hostname: window.location.hostname,
    href: window.location.href,
  })}`
}

/**
 * Returns a promise that resolves when the DOM is loaded (does not wait for images to load)
 */
async function domIsReady () {
  // already loaded
  if (['interactive', 'complete'].includes(document.readyState)) {
    return
  }
  // wait for load
  return new Promise((resolve) =>
    window.addEventListener('DOMContentLoaded', resolve, { once: true })
  )
}
yqrashawn commented 4 years ago

The bug will disappear if I change

(js2-mode)
(evil-mode)
(evil-matchit-mode)

to

(evil-mode)
(evil-matchit-mode)
(js2-mode)
redguardtoo commented 4 years ago

C-h k %, should be evilmi-jump-item. Besides, I'm not sure how you set up auto-mode-alist, but it looks your major mode is js-mode by default.

Check my README. You can use (global-evil-matchit-mode 1) to turn on it or add (evil-matchit-mode 1) in your major mode hook.

yqrashawn commented 4 years ago

I changed the init.debug.el to this

(setq load-prefer-newer t)
(when (fboundp 'tool-bar-mode)
  (tool-bar-mode -1))
(toggle-scroll-bar -1)
(menu-bar-mode -1)
(package-initialize)
(setq scroll-bar-background nil)

(setq url-proxy-services
      '(("http" . "127.0.0.1:6152")
        ("https" . "127.0.0.1:6152")))
(setq gc-cons-threshold 100000000)

(defun my-minibuffer-setup-hook ()
  (setq gc-cons-threshold most-positive-fixnum))
(defun my-minibuffer-exit-hook ()
  (setq gc-cons-threshold (* 8 1024 1024)))
(add-hook 'minibuffer-setup-hook #'my-minibuffer-setup-hook)
(add-hook 'minibuffer-exit-hook #'my-minibuffer-exit-hook)

(let ((bootstrap-file (concat user-emacs-directory "straight/repos/straight.el/bootstrap.el"))
      (bootstrap-version 3))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(straight-use-package 'evil)
(straight-use-package 'evil-matchit)
(straight-use-package 'js2-mode)
(straight-use-package 'rjsx-mode)
(straight-use-package 'typescript-mode)

(evil-mode 1)
(global-evil-matchit-mode 1)
(find-file (expand-file-name "~/Downloads/test.js"))

describe-variable major-mode is js-mode, describe-variable evil-matchit-mode is t, describe-key % is evilmi-jump-items, the jump is still wrong.

yqrashawn commented 4 years ago

~The function get called is evilmi-simple-get-tag~ the debug message is

evilmi--operate-on-item called => nil (point)=5116
evilmi--get-char-under-cursor called. Return: (125 5116)
evilmi-simple-get-tag called => ch=125 (point)=5116
evilmi-simple-get-tag called rlt=(5116)
evilmi-simple-jump called (point)=5116
evilmi--simple-jump called (point)=5116
evilmi--is-jump-forward called
evilmi--get-char-under-cursor called. Return: (125 5116)
evilmi--is-jump-forward return (nil nil })
evilmi--find-position-to-jump called => nil nil 125 5116
evilmi--scan-sexps called => nil
evilmi--scan-sexps called => rlt=5040 lvl=1
evilmi--adjust-jumpto called. Return: 5040
evilmi--find-position-to-jump return 5040
evilmi--adjust-jumpto called. Return: 5040
evilmi--operate-on-item called. Return: 5041

The 5040 is the right point { the 5041 is the new line character

redguardtoo commented 4 years ago

fixed in 2.3.5

yqrashawn commented 4 years ago

Thanks!