micro-editor / micro-editor/micro

Syntax highlighting rules containing `^` conflict with section rules

Open
#1,872 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
29.6k
Forks
1.4k
Avg merge
2d 18h
Merged PRs (30d)
10

Description

Description of the problem or steps to reproduce

When a syntax highlighting rule contains the character ^, even escaped, that rule will fail to work if written after a section rule on the same line. An example highlighting syntax that causes this behaviour is as follows:

filetype: test

detect:
    filename: "\\.test$"

rules:
    - symbol.operator: "[+\\-*/^]"
    - constant.string:
        start: "\""
        end: "\""
        skip: "\\\\."
        rules: []

This causes situations like this:
Image of issue

I believe that this is caused by the following logic in /pkg/highlight/highlighter.go:

	if strings.Contains(regexStr, "^") {
		if !canMatchStart {
			return nil
		}
	}
	if strings.Contains(regexStr, "$") {
		if !canMatchEnd {
			return nil
		}
	}

The code assumes that ^ and $ are referring to the special regex characters, without any regard for whether they are being escaped. I am unfamiliar with Go, and am unsure what the best way to fix this would be.

(Because of this, I thought that this would also happen with $ and things written before a section rule, but while trying to create an example showing this, I noticed that I didn't seem to be able to get rules containing $ to match anything at all, not just when paired with a section rule. I am unsure if this is related to the problem with ^.)

Specifications

Commit hash: 49786cf8
OS: Arch Linux
Terminal: alacritty 0.5.0

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pkg/highlight/highlighter.go and reproduce the issue with the syntax definition shown in the report. Inspect how section rules determine whether regex strings can match at the start or end, then verify that escaped ^ rules work after a section rule and investigate the reported $ behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.