Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope inspector bug when extending C++ syntax highlighting through Textmate grammar #163

Open
felipejinli opened this issue Jan 2, 2022 · 4 comments

Comments

@felipejinli
Copy link

  • VS Code Version: 1.63.2
  • OS Version: Darwin x64 21.1.0

Steps to Reproduce:

  1. created a vscode extension
  2. contributed to C++ grammar by writing the following Textmate grammar file (see snippet 1) and setting the extension's package.json to contribute correctly (see snippet 2)
  3. built the extension and ran it on Extension Development Mode
  4. used Inspect Editor Scopes tool to check a token that matches to a custom defined Regex within my extended Textmate grammar file
  5. PROBLEM: inspector doesn't show my custom textmate scope

Snippet 1)

{
	"scopeName": "source.cpp",
	"fileTypes": [
		"cpp",
		"c++",
		"c"
	],
	"patterns": [
		{
			"include": "#expression"
		}
	],
	"repository": {
		"expression": {
			"patterns": [
				{
					"include": "#clog-db"
				},
				{
					"include": "#indent-DB"
				},
				{
					"include": "#array-db"
				},
				{
					"include": "#sample"
				}
			]
		},
		"clog-db": {
			"begin": "^\\s*clog",
			"end": "<<[^;]+;",
			"name": "cpdebug.clog"
		},
		"indent-DB": {
			"match": "^\\s*DB\\(\\);",
			"name": "cpdebug.indentdb"
		},
		"array-db": {
			"match": "^\\s*dba(2)?\\([^;]+\\);",
			"name": "cpdebug.arraydb"
		},
		"sample": {
			"match": "clog",
			"name": "cpdebug.sample"
		}
	}
}

Snippet 2:

	"contributes": {
			"languages": [
				{
					"id": "cpp",
					"extensions": [
						".cpp"
					]
				}
			],
			"grammars": [
				{
					"language": "cpp",
					"scopeName": "source.cpp.debug",
					"path": "./syntaxes/cpp.tmGrammar.json",
					"injectTo": [
						"source.cpp"
					]
				}
			]
		},

image

@jeff-hykin
Copy link

jeff-hykin commented Jan 2, 2022

TextMate is pretty finicky. It's possible it's an inspect scope issue, but I'd bet it's a pattern matching issue.

Might want to check if it works when you move the clog outside of main. (Which I know sounds dumb cause you'd never use it top level but it may reveal the issue)

I maintain the C++ syntax so I feel obligated to say injections are not going to be fun. You might be able to get away with something simple like clog though. The ^\\s* you've makes me assume you know what you're doing though 👍.

@felipejinli
Copy link
Author

felipejinli commented Jan 2, 2022

I've tried your suggestion (which is smart since the hypothesis could've been another C++ language rule parsing the main and thus not using my custom expression language rule)

However, the scope inspector is still unable to pick up my custom scope.

Was thinking of using a derived scopename like source.cpp.cpdebug since that would give my language rules higher precedence than that of C++'s original but it doesn't work.

I've uploaded my vscode extension code to the following repo (https://github.com/felipejinli/vscode-hide-logs). Would you mind taking a look? Really appreciate your help :)

@jeff-hykin
Copy link

Currently I probably can't take a good look at it. Most of the time textmate scope issues require trial an error to figure it out even for the best of us.

In 4 months I might be doing some work on the C++ syntax. I've wanted injections in the past so I might build-in some handy scopes like a source.cpp#inject-everywhere to make this process easier.

@RedCMD
Copy link

RedCMD commented Sep 18, 2024

same as in https://stackoverflow.com/a/78961689/12817801
you are trying to override the C++ grammar
when you should instead be injecting into it

Remove "languages": [] and "language": "cpp"
Add an "injectionSelector" to your grammar file

"injectionSelector": "L:source.cpp -string -comment"

image

https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#injection-grammars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants