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

#pragma #define not keywords (C++) and dspic reserved registers #2439

Closed
blotfi opened this issue Jun 28, 2020 · 11 comments
Closed

#pragma #define not keywords (C++) and dspic reserved registers #2439

blotfi opened this issue Jun 28, 2020 · 11 comments

Comments

@blotfi
Copy link

blotfi commented Jun 28, 2020

Language
C++ for dspic Microchip microcontrollers

Additional resources
I would like to add reserved registers names to be displayed with a special colors
Can I put a .json containing these reserved registers (example _LATB15, _LATB12, _PLLPRE, _PLLPOST... which are in a .h file) to be displayed in another color
we can have C++ instruction like:

       #ifndef __SIMUL_ISIS
        while (OSCCONbits.COSC != 0b011); // Wait for Clock switch to occur
        while (OSCCONbits.LOCK != 1); // Wait for PLL to lock, only if PLL is needed
       #endif

compilation directives (#ifndef #endif) should be in a color, its text in another color
while is a keyword
OSCCONbits.COSC is register reserved keyword

I tried "Highlight Keywords" plugin but #xxxx is not recognised as keyword

@blotfi
Copy link
Author

blotfi commented Jun 28, 2020

.token.directive, .token.property {
	color: #F1FA8C;
}

but I need to let the property be evaluated as an expression
exactly like jetbrains CLion treats its C files

@RunDevelopment
Copy link
Member

Ok. Basically, you want 2 things:

First. You need to highlight some global constants/variables.

The easiest way to achieve this is by modifying the C++ language definition. Run the following code after you loaded Prism and before you do any highlighting (e.g. by appending it your prism.js file form the download page).

Prism.languages.insertBefore('cpp', 'constant', {
    'dspic-register': /\b(?:_LATB15|_LATB12|...)\b/
});

The above will highlight all your registers with the dspic-register class.

Secondly:

I need to let the property be evaluated as an expression exactly like jetbrains CLion treats its C files

What do you mean by "be evaluated", we only do highlighting?
And how does jetbrains CLion treat C files (I never used the IDE)?

@RunDevelopment
Copy link
Member

compilation directives (#ifndef #endif) should be in a color, its text in another color

What do you mean by that? Aren't they already?
image

@blotfi
Copy link
Author

blotfi commented Jun 28, 2020

compilation directives (#ifndef #endif) should be in a color, its text in another color

What do you mean by that? Aren't they already?
image

#ifdef in one color
foo in another
like
.token.property {
color: #a9b7c6;
}
but here # is not the same color as ifdef whereas in C editors it is:
also some directive, like #pragma have expression like

#pragma config BWRP = WRPROTECT_OFF

or

#define MILLISEC FCY/20000

so the expression should be colored (20000 as number in blue..)

@blotfi
Copy link
Author

blotfi commented Jun 28, 2020

The above will highlight all your registers with the dspic-register class.

Secondly:

I need to let the property be evaluated as an expression exactly like jetbrains CLion treats its C files

What do you mean by "be evaluated", we only do highlighting?

yes sorry I mean syntax highlighting but as expression (numbers)

And how does jetbrains CLion treat C files (I never used the IDE)?

see the pdf file here
cmde_c.pdf

I am using this slightly modified css to acheive this but it is not perfect as the pdf files (from CLion)
prism-darcula.zip

@blotfi
Copy link
Author

blotfi commented Jun 28, 2020

dspic-register

    Prism.languages.insertBefore('cpp', 'constant', {
        'dspic-register': /\b(?:_PLLPOST|_LATB15)\b/
    });
    Prism.languages.insertBefore('c', 'constant', {
        'dspic-register': /\b(?:_PLLPOST|_LATB15)\b/
    });

this works but not every where for example in the #define property it is not highlighted (see my previous post)

@RunDevelopment
Copy link
Member

I see. So for directives, we need 2 changes: We need to include the #, so it's highlighted as part of the directive's name, and we have to highlight preprocessor expressions. Is that correct?

@RunDevelopment
Copy link
Member

With #2440, macro expressions are now highlighted.

image

(This will also highlight your registers in macros accordingly.)
(The # of directives isn't highlighted by default to preserve the old look, but it's tokenized now, so you can add a CSS rule to give it a different color.)

After #2440, you should be able to style your C++ code similar to your screenshots (they were very helpful btw!).

If you want 100% accuracy: With Custom class, you can add additional classes to tokens, so you could add a special class just for semicolons:

Prism.plugins.customClass.add(({content, type, language}) => {
	if (content === ';' && type === 'punctuation' && language === 'cpp') {
		return 'semicolon';
	}
});

It might take some time for #2440 to get merged, so you might want to branch it.

@blotfi
Copy link
Author

blotfi commented Jun 29, 2020

I see. So for directives, we need 2 changes: We need to include the #, so it's highlighted as part of the directive's name, and we have to highlight preprocessor expressions. Is that correct?

yes, that's correct
Can I do it by myself using a .js executed after prismjs load?
or do I have to modify the src version directly

thanks

@RunDevelopment
Copy link
Member

Can I do it by myself using a .js executed after prismjs load?
or do I have to modify the src version directly

Both will work.

<script src="prism.js"></script>
<script>/* The insertBefore calls */</script> 

or

/* PrismJS 1.20.0
https://prismjs.com/download.html#... */
...
/* The insertBefore calls */

@RunDevelopment
Copy link
Member

The issue seems to have been resolved but I forgot to close it.

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

No branches or pull requests

2 participants