Recent

Author Topic: Identifier capitalization?  (Read 790 times)

Peter H

  • Full Member
  • ***
  • Posts: 141
Identifier capitalization?
« on: November 30, 2020, 12:28:27 pm »
Hello,

I want for all Identifiers in a progamm to make the capitalization equal to the declaration.

Example:

var
   VeryImportantNumber:integer;

"if veryimportantnumber = 0 then"
should become
"if VeryImportantNumber = 0 then"

and this for all identifiers in the program.
Is this possible?

Handoko

  • Hero Member
  • *****
  • Posts: 4017
  • My goal: build my own game engine using Lazarus
Re: Identifier capitalization?
« Reply #1 on: November 30, 2020, 12:38:51 pm »
Use Find & Replace feature:
Source Code Editor > Ctrl + F > fill the Text to find field > Enable the Replace with checkbox and provide the identifier with the correct capitalization > Disable Case sensitive checkbox > Set the Origin option to From beginning > Set the Search scope option to Global > Set the Direction to Forward search > Click the Replace All button

If you want to do it on multiple files:
Lazarus main menu > Search > Find in files

I'm not sure, I don't use it but I think you can also try JEDI Code Format:
Lazarus main menu > Source > JEDI Code Format
« Last Edit: November 30, 2020, 12:49:54 pm by Handoko »

Peter H

  • Full Member
  • ***
  • Posts: 141
Re: Identifier capitalization?
« Reply #2 on: November 30, 2020, 12:57:24 pm »
Yes thank you, but this will only work for a single identifier.

I think this can also (and better) be done with "Refactor" but this also works on a single identifier.
I tried Jedi Code format, this doesnt do it, it only unifies the capitalization for keywords.

I watched on Youtube tutorial videos for delphi where identifiers are automatically correctly capitalized during or after typing and I asked myself if there is a similar feature or tool for freepascal/Lazarus.

Thereoretically the compiler could do this, because the compiler knows and understands all declarations.
« Last Edit: November 30, 2020, 01:04:21 pm by Peter H »

PascalDragon

  • Hero Member
  • *****
  • Posts: 2611
  • Compiler Developer
Re: Identifier capitalization?
« Reply #3 on: November 30, 2020, 01:03:38 pm »
I watched on Youtube tutorial videos for delphi where identifiers are automatically correctly capitalized during or after typing and I asked myself if there is a similar feature or tool for freepascal.

Lazarus does that as well when you use its code completion (Default: Ctrl + Space) functionality (if there is only one valid identifier it will complete that without showing the code completion window).

Thereoretically the compiler could do this, because the compiler knows all declarations.

No, the compiler does not modify source files.

Handoko

  • Hero Member
  • *****
  • Posts: 4017
  • My goal: build my own game engine using Lazarus
Re: Identifier capitalization?
« Reply #4 on: November 30, 2020, 01:04:56 pm »
I tried Jedi Code format, this doesnt do it, it only unifies the capitalization for keywords.

You can configure it:
Lazarus main menu > Source > JEDI Code Format > Format Settings > left panel: Clarify > Capitalization > Identifiers

I watched on Youtube tutorial videos for delphi where identifiers are automatically correctly capitalized during or after typing and I asked myself if there is a similar feature or tool for freepascal.

Just as PascalDragon said. I always use Ctrl+Space when typing identifier so my codes are always capitalized uniformly.

Peter H

  • Full Member
  • ***
  • Posts: 141
Re: Identifier capitalization?
« Reply #5 on: November 30, 2020, 01:09:04 pm »
Thank you, I will try it.

Of course the compiler should not modify the source.
However, theoretically it could emit a new file with correct capitalization, if there where a comand line option.
This is just an idea of mine, I know it is not possible currently.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2611
  • Compiler Developer
Re: Identifier capitalization?
« Reply #6 on: November 30, 2020, 01:12:05 pm »
However, theoretically it could emit a new file with correct capitalization, if there where a comand line option.
This is just an idea of mine, I know it is not possible currently.

That would be a completely unnecessary complication of the compiler who currently does not care about the case of identifiers at all. Use tools that have the purpose of adjusting the source and let the compiler simply do its job.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1165
    • formatio reticularis
Re: Identifier capitalization?
« Reply #7 on: November 30, 2020, 01:23:52 pm »
That would be a completely unnecessary complication of the compiler who currently does not care about the case of identifiers at all. Use tools that have the purpose of adjusting the source and let the compiler simply do its job.

Fully agree. This would be the realm of separate tools.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.10 | FPC 3.2.0 | PPC, Intel, ARM | macOS, Windows, Linux

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3938
  • I like bugs.
Re: Identifier capitalization?
« Reply #8 on: November 30, 2020, 01:30:47 pm »
Fully agree. This would be the realm of separate tools.
It would be a job for Codetools. The "Rename identifier..." feature already does it for a single variable. A new feature "Match capitalization of identifiers..." or similar could be added.
It will be accepted for sure if somebody implements it.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 1787
Re: Identifier capitalization?
« Reply #9 on: December 16, 2020, 03:40:20 pm »
Fully agree. This would be the realm of separate tools.
It would be a job for Codetools. The "Rename identifier..." feature already does it for a single variable. A new feature "Match capitalization of identifiers..." or similar could be added.
It will be accepted for sure if somebody implements it.

That would be nice. I know that case sensitivity in Pascal tends to... excite strong emotions and heated discourse, but nobody in their right mind could object (surely!) to a helper which made sure that capitalisation was consistent.

But I'd imagine that it would be far more difficult for Pascal than for C/C++, on account of the language's support for nested functions.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9022
  • FPC developer.
Re: Identifier capitalization?
« Reply #10 on: December 16, 2020, 03:49:38 pm »
Thank you, I will try it.

Of course the compiler should not modify the source.
However, theoretically it could emit a new file with correct capitalization, if there where a comand line option.

No. The/A compiler does not read a file into a representation that can be converted back to formatted source. E.g. it doesn't even look at parts that are in an inactive IFDEF. 

For that there are source to source tools like jedi formatter, fcl-passrc etc.
 

MarkMLl

  • Hero Member
  • *****
  • Posts: 1787
Re: Identifier capitalization?
« Reply #11 on: December 17, 2020, 10:49:43 pm »
No. The/A compiler does not read a file into a representation that can be converted back to formatted source. E.g. it doesn't even look at parts that are in an inactive IFDEF. 

I think that saying "a compiler" there is pushing credibility.

A C/C++ compiler with integrated preprocessor would emit the original file if not told to make changes.

The parser in a compiler normally stores at least one line verbatim so that it can point to the location of an error message, and it would not be difficult to output each line after it had been processed.

A code listing from a compiler normally has the original source in a form that can be extracted.

OK, so FPC doesn't do it like that... but there is no reason why in the general case a single-pass compiler shouldn't emit corrected variable names as it runs, since the symbol table is built before code is generated.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9022
  • FPC developer.
Re: Identifier capitalization?
« Reply #12 on: December 17, 2020, 10:58:34 pm »
No. The/A compiler does not read a file into a representation that can be converted back to formatted source. E.g. it doesn't even look at parts that are in an inactive IFDEF. 

I think that saying "a compiler" there is pushing credibility.

A C/C++ compiler with integrated preprocessor would emit the original file if not told to make changes.

The parser in a compiler normally stores at least one line verbatim so that it can point to the location of an error message, and it would not be difficult to output each line after it had been processed.

Without wanting to split hairs about details of what constitutes a compiler, I yield.

Have fun with your split hairs.

 

TinyPortal © 2005-2018