Utils - skalogryz/editorConfig GitHub Wiki
Utilities unit provide the functions to actually load a .editorconfig file (populating TEditorConfigFile class), perform file lookup, as well as .ini reading utilities.
Utilities unit depends on RTL units: Classes and SysUtils.
If your project is avoiding those units you might not want to use the unit at all. Yet, it would provide you an example of implementation.
function GetTabWidth(ec: TEditorConfigEntry; const defVal: integer = -1): integer;
Per editorConfigs specs, if tab_width property is undefined, indent_size should be used. The function checks for the definition of both properties and returns the corresponding value.
function isTabIndent(const indent_style: string): Boolean;
otherwise returns false
function isSpaceIndent(const indent_style: string): Boolean;
otherwise returns false
type
TLookUpResult = record
editorConfigFile: string;
filename_pattern: string;
indent_style : string;
indent_size : integer;
tab_width : integer;
end_of_line : string;
charset : string;
trim_trailing_whitespace : TTriBool;
insert_final_newline : TTriBool;
end;
The structure is used to return the result of successful look up performed.
- editorConfigFile - the full file name of .editorconfig file where the configuration was identified
- filename_pattern - the name of the pattern that specifies the setting (corresponds to name of TEditorConfigEntry)
- all other fields are copy of the data fields of TEditorConfigEntry
function LookupEditorConfig(const FileName: RawByteString; out res: TLookUpResult; IgnoreCase: Boolean = true): Boolean; overload;
The following overloaded functions
function LookupEditorConfig(const FileName: UnicodeString; out res: TLookUpResult; IgnoreCase: Boolean = true): Boolean; overload;
function LookupEditorConfig(const FileName: WideString; out res: TLookUpResult; IgnoreCase: Boolean = true): Boolean; overload;
function FindMatching(const SrchFileName: string; cfg: TEditorConfigFile; IgnoreCase: Boolean): TEditorConfigEntry;
- IgnoreCase - would ignore the file case when doing pattern matching
procedure InitLookupResult(out lk: TLookupResult);
Reading functions are provided for convenience.
procedure ReadFromFile(dst: TEditorConfigFile; const fn: string; usePath: Boolean = true);
- Reads the .editorconfig from a file stream, where file name is specified by fn property. The file is opened for read, allowing other programs to have any access to the file. The further loading is passed to ReadFromStream()
- usePath indicates if filepath field of TEditorConfigFile should be populated with the value passed with fn
- no exceptions are handled by the procedure.
procedure ReadFromStream(dst: TEditorConfigFile; src: TStream);
- Reads the .editorconfig from stream. The stream is read into TStrings and the data is acutally read from ReadFromStrings()
- The procedure would not suppress any exceptions thrown during the stream access.
procedure ReadFromStrings(dst: TEditorConfigFile; str: TStrings);
- Travels string from top to bottom, using ini parsing routines
- Each header found becomes an entry in dst editor config. Any invalid or non editorConfig key/values are ignored
- Only root key/value recognized out of the header. Any other key values met are ignored.
NOTE: in future, those routines might be removed entirely for the unit.
These routines are provided only because FPC doesn't provide any easy way to extend IniFiles unit to support "#" char for line comments. (Which is a norm for .editorconfig). Plus the way .editorconfig file is read, doesn't require the hold the entire .ini structure in memory at all time.
Over all the section doesn't really need to be publicly available, yet is still provided, if default ReadXXX routines are not satisfying the requirements.
function ParseIniLine(
const iniline: string;
const cfg: TIniSettings;
out line: TIniLine
): Boolean;
- iniline - a line of text from .ini file
- cfg - the settings to parse an .ini file
- line - the description of line parsed
Keep in mind of the "root" value that's a special exception for .editorConfig files.
The structure describes the parsed .ini file line
TIniLine = record
ltype : TIniLineType;
value : string;
key : string;
end;
- ltype - type of the line identified:
- iltComment - it's a comment line, the first character matched the list of comment characters
- iltKeyValue - it's a key/value pair line "somename=value", value and key are populated with their repsective values
- iltHeader - it's a header line, like: [name]. Only value field is populated with the name of the header (excluding brackets)
- iltOther - an empty or invalid line
The structure that holds options for parsing .editorconfig (.ini) file.
The only option supported right now is the character that starts a comment line.
The function initialized TIniSettings structure.
procedure IniSetting(out ini: TIniSettings; const AComment: TAnsiCharSet);
- AComment - specified Ansi-characters set to be used for identifying comments.