Recent

Author Topic: Loading and Saving TSynAnySyn attribute configuration  (Read 3984 times)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 258
Loading and Saving TSynAnySyn attribute configuration
« on: June 29, 2017, 09:46:06 pm »
Lazarus 1.6.4
FPC 3.0.2
Apple macOS 10.12.5 Sierra

Hello, I am currently working on a code preview utility for the Golang language. As far as I can tell, there is not yet a Syntax component written for Golang, so I have been using the TSynAnySyn component with the Golang keywords loaded, as this works very well. I do play to convert this into a TSynGolangSyn component at some stage, but first things first!

In an attempt to make my application user-friendly, I am adding the ability to edit the foreground and background colours of the various attribute types: CommentAttri, IdentifierAttri and so on. I am now trying to decide how to store and transfer this data as a single data structure. I have noticed in the source of TSynAnySyn, that there is a method called LoadHighlighter which loads the attribute information from a file. However, there is no corresponding SaveHighlighter method to perform the reverse operation. I see also in the ancestor component TSynCustomHighlighter that there are LoadFromFile and SaveToFile methods which could be useful.

The problem I have is that these methods tie me into a particular way of storing the data on disk, whereas what I need is a way of saving the attribute data to an in-memory structure which I can then choose to save in a method of my choosing. Actually, in the course of writing this message, I think I have talked myself out of trying to use the built-in methods, and instead, I think I will roll my own config mechanism. This makes sense because I am already using JSON for some other config information, so it would be neat to use the same approach for the attribute data.

I'm interested to hear anyone's thoughts on this, assuming you've made it through my ramblings!

Thanks,
Carl Caulkett
macOS 10.14.1
Lazarus 2.0.0RC2 Carbon / Lazarus 2.1.0 r59743 Cocoa
FPC 3.0.4

Edson

  • Hero Member
  • *****
  • Posts: 1041
Re: Loading and Saving TSynAnySyn attribute configuration
« Reply #1 on: June 30, 2017, 04:24:24 am »
Maybe you can use the SynFacilSyn highlighter instead of SynAnySyn. It uses an XML file where the syntax and the token attributes are defined.

I use it and when I want to change the attributes, I modifie the XML file and reload the highlighter from disk.
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 258
Re: Loading and Saving TSynAnySyn attribute configuration
« Reply #2 on: June 30, 2017, 08:21:44 am »
Hi Edson, thanks for the reply. I'll check out TSynFacilSyn.
macOS 10.14.1
Lazarus 2.0.0RC2 Carbon / Lazarus 2.1.0 r59743 Cocoa
FPC 3.0.4

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: Loading and Saving TSynAnySyn attribute configuration
« Reply #3 on: June 30, 2017, 01:11:54 pm »
Just in case, there also is a tutorial on writing your own HL: http://wiki.lazarus.freepascal.org/SynEdit_Highlighter

Edson

  • Hero Member
  • *****
  • Posts: 1041
Re: Loading and Saving TSynAnySyn attribute configuration
« Reply #4 on: June 30, 2017, 05:14:45 pm »
Hi Edson, thanks for the reply. I'll check out TSynFacilSyn.

Like a sample this is a frame I use to use modify attributes on SynFacilSyn. It really works with SynFacilCompletion (a highlighter derived from SYnFacilSyn, who includes code-completion), but should work with SynFacilSyn.  It is prepared to manage several XML syntax file, but can work with only one, too.

« Last Edit: June 30, 2017, 05:17:21 pm by Edson »
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 258
Re: Loading and Saving TSynAnySyn attribute configuration
« Reply #5 on: June 30, 2017, 11:43:42 pm »
Hi Edson, since you seem to be very knowledgeable about the inner workings of SynEdit and the associated components, perhaps you could answer another question I have.

The TSynAnySyn component has 13 attribute properties:

CommentAttri
ConstantAttri
DotAttri
EntityAttri
IdentifierAttri
KeyAttri
NumberAttri
ObjectAttri
PreprocessorAttri
SpaceAttri
StringAttri
SymbolAttri
VariableAttri

Is there any way of getting a reference to the property from a name string, so that, for example, NameToAttribute('Preprocessor') would return the PreprocessorAttri property. Obviously, I could do the equivalent of a large switch statement but I was hoping to do something a bit more streamlined, maybe using RTTI perhaps. I notice that the attribute properties are TPersistent descendants so maybe some kind of FindClass type mechanism would work here?
macOS 10.14.1
Lazarus 2.0.0RC2 Carbon / Lazarus 2.1.0 r59743 Cocoa
FPC 3.0.4

Edson

  • Hero Member
  • *****
  • Posts: 1041
Re: Loading and Saving TSynAnySyn attribute configuration
« Reply #6 on: July 01, 2017, 12:20:56 am »
I don't use SynAnySyn, but all SynEdit highlighter stores his attributes in  the propertie Attribute[], so you can iterate, to find some that have a particular name.

Hi Edson, since you seem to be very knowledgeable about the inner workings of SynEdit and the associated components

Actually, the expert and maintainer is Martin_fr  :-X. I'm the creator of SynfacilSyn. But anyone here can help, according of his knowledge of SynEdit.
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: Loading and Saving TSynAnySyn attribute configuration
« Reply #7 on: July 01, 2017, 02:26:09 am »
All Highlighters have

Code: Pascal  [Select]
  1. for i := 0 to HL.AttrCount-1 do begin
  2.   a := HL.Attribute[i];
  3.   n := a.Name;  // displayname, changes with language
  4.   j := a.StoredName; // fixed name for xml, json etc
  5. end;
  6.  

If you want to find it by name, then you need to iterate then yourself.

You can find the names in TSynAnySyn.Create
Code: Pascal  [Select]
  1. fCommentAttri := TSynHighlighterAttributes.Create(@SYNS_AttrComment, SYNS_XML_AttrComment);
just use codetools to lookup the constant.
« Last Edit: July 01, 2017, 02:29:15 am by Martin_fr »