ScintillaGadget : Colorer les commentaires d'un code JS

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

ScintillaGadget : Colorer les commentaires d'un code JS

Message par falsam »

Me voici à nouveau avec une nouvelle problématique liée au GadgetScintilla()

Je cherche à colorer les commentaires d'un code Javascript.

1 - un commentaire sur une seule ligne est précédé d'un double-slash //.
// texte de commentaire
2 - Un commentaire qui s'étend sur plusieurs lignes est précédé de /* et suivis par */.
/* Ceci est un commentaire sur plusieurs lignes.
Il peut être de n'importe quelle longeur. */
:!: Pour le moment je me concentre sur la cas numéro 1.

■ Voici le code de test

Code : Tout sélectionner

EnableExplicit

Enumeration Highlight
  #Style_Comment
  #Style_Bracket
  #Style_NonKeyword
EndEnumeration

Enumeration Form
  #mf
EndEnumeration

Enumeration Gadget  
  #Editor
EndEnumeration

Declare Start()

Declare MainFormShow() 
Declare MainFormResize()
Declare MainFormClose()

Declare ScintillaProperties(Gadget)
Declare ScintillaCallBack(Gadget, *scinotify.SCNotification)
Declare ScintillaHighlight(Gadget.l, EndPos.l)
Declare ScintillaGetLineEndPosition(Gadget, Line)
Declare ScintillaLineFromPosition(gadget, Pos)

Start()

Procedure Start()
  OpenWindow(#mf,  0,  0, 1024, 768, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If InitScintilla()
    ScintillaGadget(#Editor, 0, 0, WindowWidth(#mf), WindowHeight(#mf), @ScintillaCallBack())
    ScintillaProperties(#Editor)
    SetActiveGadget(#Editor)
  EndIf
      
  BindEvent(#PB_Event_CloseWindow, @MainFormClose())
  
  Repeat : WaitWindowEvent(10) : ForEver
EndProcedure

Procedure MainFormClose()
  End  
EndProcedure

Procedure ScintillaProperties(Gadget)
  ScintillaSendMessage(#Editor, #SCI_SETCODEPAGE, #SC_CP_UTF8)

  ScintillaSendMessage(Gadget, #SCI_STYLESETBACK, #STYLE_DEFAULT, RGB(175, 175, 175))
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #Style_Comment, RGB(255, 255, 255))
  ScintillaSendMessage(Gadget, #SCI_STYLESETFONT,#STYLE_DEFAULT, @"Lucida Console")       ;Police à utiliser 
  ScintillaSendMessage(Gadget, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 11)                     ;Taille de la police
  ScintillaSendMessage(Gadget, #SCI_STYLECLEARALL)
  
  ;Line numbering column 
  ScintillaSendMessage(Gadget, #SCI_SETMARGINTYPEN, 0, #SC_MARGIN_NUMBER)                
  ScintillaSendMessage(Gadget, #SCI_SETMARGINWIDTHN, 0, 50)                               
  ScintillaSendMessage(Gadget, #SCI_STYLESETBACK, #STYLE_LINENUMBER, RGB(154, 205, 50))   
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #STYLE_LINENUMBER, RGB(82, 86, 111))    
  
  ;Comment
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #Style_Comment, RGB(0, 128, 0))
  
  ;Tag
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #Style_Bracket, RGB(255, 0, 0))
  
  ;Default
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #Style_NonKeyword, RGB(255, 255, 255))
  
EndProcedure

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)  
  Select *scinotify\nmhdr\code    
    Case #SCN_CHARADDED     
      
    Case #SCN_STYLENEEDED
      ScintillaHighlight(Gadget, *scinotify\position)
      
  EndSelect  
EndProcedure

Procedure ScintillaHighlight(Gadget.l, EndPos.l)
  Protected Char, CurrentPos.i = 0, EndLinePos.i,  StartKeyword.i 
  
  ScintillaSendMessage(Gadget, #SCI_STARTSTYLING, CurrentPos, $1F | #INDICS_MASK)
  
  While CurrentPos <= EndPos
    Char = ScintillaSendMessage(Gadget, #SCI_GETCHARAT, CurrentPos)  
    
    Select Char              
      Case '{', '}' 
        Debug "Styling { or }"
        ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 1, #Style_Bracket)          
        
      Case '/' ;Comment
        ;Is the preceding character a /?
        If ScintillaSendMessage(Gadget, #SCI_GETCHARAT, currentpos - 1) = '/'
          Debug "Styling comment"
          currentpos - 1
          EndLinePos = ScintillaGetLineEndPosition(Gadget, ScintillaLineFromPosition(Gadget, currentpos+1))  
          StartKeyword = 1
          While currentpos < EndLinePos
            currentpos + 1
            StartKeyword + 1
          Wend
          ScintillaSendMessage(Gadget, #SCI_SETSTYLING, StartKeyword, #Style_Comment)
          
        Else
          ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 0, #Style_NonKeyword)
        EndIf
        
      Default
        ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 1, #Style_NonKeyword)
        
    EndSelect
    
    currentpos+1
  Wend 
EndProcedure

Procedure ScintillaGetLineEndPosition(Gadget, Line)
  ProcedureReturn ScintillaSendMessage(Gadget, #SCI_GETLINEENDPOSITION, Line)
EndProcedure

Procedure ScintillaLineFromPosition(gadget, Pos)
  ProcedureReturn ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, Pos)
EndProcedure
Pour voir ce qu'il se passe avec les commentaires, insérer le code JavaScript suivant

Code : Tout sélectionner

// Get the canvas element (#renderCanvas)
var canvas = document.getElementById("renderCanvas");  

// Generate the BABYLON 3D engine
var engine = new BABYLON.Engine(canvas, true); 

// Add the create scene function 
var createScene = function () {

    // Create the scene space
    var scene = new BABYLON.Scene(engine);

    // Add a camera to the scene and attach it to the canvas
    var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 10, BABYLON.Vector3.Zero(), scene);
    camera.attachControl(canvas, true);

    // Add lights to the scene
    var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
    var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);

    // Add and manipulate meshes in the scene
    var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", { diameter:1 }, scene);

    return scene;
 };

// End of the create scene function  : Call the createScene function   
 var scene = createScene(); 

// Register a render loop to repeatedly render the scene
 engine.runRenderLoop(function () { 
    scene.render();
});

// Watch for browser/canvas resize events
window.addEventListener("resize", function () { 
    engine.resize();
});

-Certains commentaires ne sont pas colorés jusqu'à la fin de la ligne.
-Les brackets {} ne sont pas colorés correctement.

:idea: Si on supprime la ligne 14, la coloration syntaxique est correcte.

J'espére que vous allez pouvoir m'aider. Merci.

■ La solution est donnée par Zorro : Lors du processus de coloration syntaxique, quand le process repère le caractère "/" il faut vérifier que le caractère suivant est lui aussi un "/".

Code : Tout sélectionner

Case '/' ; By Dobro
        If ScintillaSendMessage(Gadget, #SCI_GETCHARAT, CurrentPos+1) ='/'
Dans mon code je faisais le contraire.

Code : Tout sélectionner

Case '/' ;Comment
        ;Is the preceding character a /?
        If ScintillaSendMessage(Gadget, #SCI_GETCHARAT, currentpos - 1) = '/'
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Zorro
Messages : 2185
Inscription : mar. 31/mai/2016 9:06

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par Zorro »

voila pour les commentaires simple

Code : Tout sélectionner

 EnableExplicit

Enumeration Highlight
  #Style_Comment
  #Style_Hug
  #Style_NonKeyword
EndEnumeration


Enumeration Form
  #mf
EndEnumeration

Enumeration Gadget 
  #Editor
EndEnumeration

Declare Start()

Declare MainFormShow()
Declare MainFormResize()
Declare MainFormClose()

Declare ScintillaProperties(Gadget)
Declare ScintillaCallBack(Gadget, *scinotify.SCNotification)
Declare ScintillaHighlight(Gadget.l, EndPos.l)
Declare ScintillaGetLineEndPosition(Gadget, Line)
Declare ScintillaLineFromPosition(gadget, Pos)

Start()

Procedure Start()
  OpenWindow(#mf,  0,  0, 1024, 768, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  If InitScintilla()
    ScintillaGadget(#Editor, 0, 0, WindowWidth(#mf), WindowHeight(#mf), @ScintillaCallBack())
    ScintillaProperties(#Editor)
    SetActiveGadget(#Editor)
  EndIf
     
  BindEvent(#PB_Event_CloseWindow, @MainFormClose())
 
  Repeat : WaitWindowEvent(10) : ForEver
EndProcedure

Procedure MainFormClose()
  End 
EndProcedure

Procedure ScintillaProperties(Gadget)
  ScintillaSendMessage(Gadget, #SCI_STYLESETBACK, #STYLE_DEFAULT, RGB(175, 175, 175))
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #Style_Comment, RGB(255, 255, 255))
  ScintillaSendMessage(Gadget, #SCI_STYLESETFONT,#STYLE_DEFAULT, @"Lucida Console")       ;Police à utiliser
  ScintillaSendMessage(Gadget, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 11)                     ;Taille de la police
  ScintillaSendMessage(Gadget, #SCI_STYLECLEARALL)
 
  ;Line numbering column
  ScintillaSendMessage(Gadget, #SCI_SETMARGINTYPEN, 0, #SC_MARGIN_NUMBER)               
  ScintillaSendMessage(Gadget, #SCI_SETMARGINWIDTHN, 0, 50)                               
  ScintillaSendMessage(Gadget, #SCI_STYLESETBACK, #STYLE_LINENUMBER, RGB(154, 205, 50))   
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #STYLE_LINENUMBER, RGB(82, 86, 111))   
 
  ;Comment
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #Style_Comment, RGB(0, 128, 0))
 
  ;Tag
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #Style_Hug, RGB(255, 0, 0))
 
  ;Default
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #Style_NonKeyword, RGB(255, 255, 255))
 
EndProcedure

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
  Protected SciCurrentPos, SciWordStartPos, KeyWords.s
 
  Protected Prefix.s, i
 
  Select *scinotify\nmhdr\code   
    Case #SCN_CHARADDED     
     
    Case #SCN_STYLENEEDED
      ScintillaHighlight(Gadget, *scinotify\position)
     
  EndSelect 
EndProcedure

Procedure ScintillaHighlight(Gadget.l, EndPos.l)
  Protected Char, CurrentPos.i = 0, EndLinePos.i,  StartKeyword.i
 
  ScintillaSendMessage(Gadget, #SCI_STARTSTYLING, CurrentPos, $1F | #INDICS_MASK)
 
  While CurrentPos <= EndPos
    Char = ScintillaSendMessage(Gadget, #SCI_GETCHARAT, CurrentPos) 
   
    Select Char             
      Case '{', '}'
        Debug "Styling { or }"
        ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 1, #Style_Hug)         
       
      Case '/' ;Comment
        char = ScintillaSendMessage(Gadget, #SCI_GETCHARAT, currentpos ) ;- 1
        If ScintillaSendMessage(Gadget, #SCI_GETCHARAT, currentpos - 1) = '/'
          Debug "Styling comment"
          currentpos - 1
          EndLinePos = ScintillaGetLineEndPosition(Gadget, ScintillaLineFromPosition(Gadget, currentpos)) 
          StartKeyword = 1
          While currentpos < EndLinePos
            currentpos + 1
            ;char = ScintillaSendMessage(Gadget, #SCI_GETCHARAT, currentpos)
            ;If char = Asc(#CRLF$)
            ;  Debug "#CRLF"
            ;  CurrentPos - 1
            ;  Break
            ;EndIf
            StartKeyword + 1
          Wend
          ScintillaSendMessage(Gadget, #SCI_SETSTYLING, StartKeyword, #Style_Comment)
        Else
          ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 0, #Style_NonKeyword)
        EndIf
       
      Default
        ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 1, #Style_NonKeyword)
       
    EndSelect
   
    currentpos+1
  Wend
EndProcedure

Procedure ScintillaGetLineEndPosition(Gadget, Line)
  ProcedureReturn ScintillaSendMessage(Gadget, #SCI_GETLINEENDPOSITION, Line)
EndProcedure

Procedure ScintillaLineFromPosition(gadget, Pos)
  ProcedureReturn ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, Pos)
EndProcedure 
Dernière modification par Zorro le jeu. 08/févr./2018 14:02, modifié 1 fois.
Image
Image
Site: http://michel.dobro.free.fr/
Devise :"dis moi ce dont tu as besoin, je t'expliquerai comment t'en passer"
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par falsam »

Merci Zorro. Mais il y a encore un souci de coloration comme tu peux le voir sur cette image.
Image
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par falsam »

@Zorro : Le commentaire sur une seule ligne doit commencer par //.
Ton code teste l'existence d'un seul / pour commencer la coloration du commentaire.

Tape /Commentaire sur une ligne dans l'editeur

Avant de commencer la coloration du commentaire, il faut tester que le caractère "/" précède bien "/"

Code : Tout sélectionner

If ScintillaSendMessage(Gadget, #SCI_GETCHARAT, currentpos - 1) = '/'
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Zorro
Messages : 2185
Inscription : mar. 31/mai/2016 9:06

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par Zorro »

Code modifié ...
Image
Image
Site: http://michel.dobro.free.fr/
Devise :"dis moi ce dont tu as besoin, je t'expliquerai comment t'en passer"
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par Kwai chang caine »

Merci à vous deux pour le partage 8)
La derniere lettre du commentaire est blanche :|
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par falsam »

Zorro a écrit :Code modifié ...
Oui mais toujours un souci de coloration.
Image
de plus :
- Le bracket n'est plus coloré en rouge
- Une parenhése est coloré alors qu'il n'existe pas de déclencheur

@Kwai chang caine : Merci de le confirmer :wink:
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par falsam »

Mise à jour du premier code : Suppression des variables inutiles pour ce test.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par Kwai chang caine »

falsam a écrit :@Kwai chang caine : Merci de le confirmer :wink:
:wink:

Image
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Avatar de l’utilisateur
Zorro
Messages : 2185
Inscription : mar. 31/mai/2016 9:06

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par Zorro »

Le probleme viens de "ScintillaSendMessage(*this\id, #SCI_SETSTYLING, numBytesToStyle, styleToUse)"

numByte to style , c'est le nombre d'octets qu'on va colorer
le probleme, c'est que maintenant Purebasic est en Unicode (j'ai toujours dit que c'etait une plaie ce truc (l'unicode) ) :roll:

Donc, il faut passer par un buffer Ascii / UTF8 (comme le fait Gosci ) ....

je m'etais fait une variante, de ton code en extrayant chaque ligne une a une
puis un scan dans la ligne en cours, pour colorer si rencontre d'un caractere "//" ou "{"

le probleme c'est qu'en faisant comme ça , on perd complétement la notion de ScintillaSendMessage(*this\id, #SCI_SETSTYLING, numBytesToStyle, styleToUse)
qui lui veux un nombre de byte a colorer , mais a partir de quoi ??? pfffff 8O

si c'est a partir du debut du text , ma technique ne peux pas marcher

c'est peut etre pour ça que la plupart des codes utilise un scan des caracteres un a un depuis le debut du text ....

mais meme là , comme on est en UTF8 , on ne peux pas compter les caracteres comme etant des "byte" a colorer
car certains caractere prennent 2 octets ... tu vois le probleme ?? d'ou les decalages en fin de ligne ... sans parler de la prise en compte de l'indentation (Espace ou Tabulation ?? )

Fred disais que c'etait facile de coder en Unicode, je continu a dire que non !
pour coder un prg jouant avec des caracteres, je reviens a Purebasic 5.42 , pour retrouver le bon vielle ASCII !!
Image
Image
Site: http://michel.dobro.free.fr/
Devise :"dis moi ce dont tu as besoin, je t'expliquerai comment t'en passer"
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par falsam »

Je ne suis pas certain que ce soit un souci unicode. Si dans mon code normal je retire cette coloration de commentaire, la coloration syntaxique fonctionne normalement. Par exemple je n'ai pas de décalage de couleur avec les mots clés.

J'ai indiqué pour cela que la page était au format UTF8.

Code : Tout sélectionner

ScintillaSendMessage(#Editor, #SCI_SETCODEPAGE, #SC_CP_UTF8)
je met le code du premier message à jour avec cette ligne manquante.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Zorro
Messages : 2185
Inscription : mar. 31/mai/2016 9:06

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par Zorro »

je crois que j'ai trouvé :)

par contre j'ai utilisé un autre code ressemblant au tiens

je ne me suis concentré que sur la partie "/" regarde bien , ça dois pouvoir s'adapter

Ajout des "{" et "}"

Code : Tout sélectionner


;===========================================;
;                                           ;
;   Scintilla example                       ;
;                                           ;
;===========================================;

Global KeyWordUp.s, KeyWordDown.s, KeyWordNone.s
KeyWordUp = "Repeat If Procedure"
KeyWordDown = "ForEver EndIf EndProcedure"
KeyWordNone = "Else"
Enumeration 0
		#LexerState_Space
		#LexerState_Comment
		#LexerState_NonKeyword
		#LexerState_Keyword
		#LexerState_FoldKeyword
		#LexerState_Constant
		#LexerState_String
		#LexerState_FoldKeywordUp
		#LexerState_FoldKeywordDown
		#LexerState_braquet
EndEnumeration

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
		InitScintilla("Scintilla.dll")
CompilerEndIf

Declare sci_getlineendposition(gadget, line)
Declare sci_linefromposition(gadget, pos)
Declare keywordis(key.s)
Declare highlight(sciptr.l, endpos.l)
Declare scintillacallback(gadget, *scinotify.scnotification)


If OpenWindow(0, 0, 0, 800, 600, "Scintilla exemple", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
		
		If UseGadgetList(WindowID(0))
				ScintillaGadget(0, 0, 0, 800, 600, @ScintillaCallBack())
				; Choose a lexer
				ScintillaSendMessage(0, #SCI_SETLEXER, #SCLEX_CONTAINER, 0)
				
				; Set default font
				ScintillaSendMessage(0, #SCI_STYLESETFONT, #STYLE_DEFAULT, @"Comic sans Ms")
				ScintillaSendMessage(0, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 12)
				ScintillaSendMessage(0, #SCI_STYLECLEARALL)
				
				; Set caret line colour
				ScintillaSendMessage(0, #SCI_SETCARETLINEBACK, $eeeeff)
				ScintillaSendMessage(0, #SCI_SETCARETLINEVISIBLE, #True)
				
				; Set styles for custom lexer
				ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Comment, $bb00)
				ScintillaSendMessage(0, #SCI_STYLESETITALIC, #LexerState_Comment, 1)
				ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_NonKeyword, 0)
				ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Keyword, RGB(0, 102, 102))
				ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Constant, RGB(169, 64, 147))
				ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_String, RGB(255, 139, 37))
				ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_braquet, RGB(255, 0, 0))
				; Margins
				ScintillaSendMessage(0, #SCI_SETMARGINTYPEN, 0, #SC_MARGIN_NUMBER)
				ScintillaSendMessage(0, #SCI_SETMARGINMASKN, 2, #SC_MASK_FOLDERS)
				ScintillaSendMessage(0, #SCI_SETMARGINWIDTHN, 0, 50)
				ScintillaSendMessage(0, #SCI_SETMARGINWIDTHN, 2, 20)
				ScintillaSendMessage(0, #SCI_SETMARGINSENSITIVEN, 2, #True)
				
				; Choose folding icons
				ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDEROPEN, #SC_MARK_CIRCLEMINUS)
				ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDER, #SC_MARK_CIRCLEPLUS)
				ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDERSUB, #SC_MARK_VLINE)
				ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDERTAIL, #SC_MARK_LCORNERCURVE)
				ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDEREND, #SC_MARK_CIRCLEPLUSCONNECTED)
				ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDEROPENMID, #SC_MARK_CIRCLEMINUSCONNECTED)
				ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDERMIDTAIL, #SC_MARK_TCORNERCURVE)
				
				; Choose folding icon colours
				ScintillaSendMessage(0, #SCI_MARKERSETFORE, #SC_MARKNUM_FOLDER, $FFFFFF)
				ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDER, 0)
				ScintillaSendMessage(0, #SCI_MARKERSETFORE, #SC_MARKNUM_FOLDEROPEN, $FFFFFF)
				ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDEROPEN, 0)
				ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDEROPENMID, 0)
				ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDERSUB, 0)
				ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDERTAIL, 0)
				ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDERMIDTAIL, 0)
				
				txt.s = "Procedure test()"+chr(10)+chr(13)
				txt.s + " #test = "+ Chr(34)+ "chaine 1"+ Chr(34)+chr(10)+chr(13)
				txt.s + " If #test = 24"+chr(10)+chr(13)
				txt.s + "   //test ******************************************************* "+chr(10)+chr(13)
				txt.s + "   //test ******************************************************* "+chr(10)+chr(13)
				txt.s + "   //test ******************************************************* "+chr(10)+chr(13)
				txt.s + "   //test ******************************************************* "+chr(10)+chr(13)
				txt.s + " Else"+chr(10)+chr(13)
				txt.s + ""+chr(10)+chr(13)
				txt.s + " EndIf"+chr(10)+chr(13)
				txt.s + "EndProcedure"+chr(10)+chr(13)
			
				ScintillaSendMessage(0, #SCI_SETTEXT, #Null, UTF8(txt.s))
		EndIf
		
		
		Repeat
				event = WaitWindowEvent()
				
				If event = #PB_Event_CloseWindow
						Quit = 1
				EndIf
				
		Until Quit = 1
		
EndIf

End


;-Procedures Zone
Procedure SCI_GetLineEndPosition(gadget, line)
		ProcedureReturn ScintillaSendMessage(gadget,#SCI_GETLINEENDPOSITION,line)
EndProcedure

Procedure SCI_LineFromPosition(gadget, Pos)
		ProcedureReturn ScintillaSendMessage(gadget,#SCI_LINEFROMPOSITION,Pos)
EndProcedure

Procedure KeyWordIs(key.s)
		Protected n
		If key=""
				ProcedureReturn -1
		EndIf
		
		For n=1 To CountString(KeyWordUp, " ") + 1
				If LCase(StringField(KeyWordUp, n, " ")) = LCase(key)
						ProcedureReturn #LexerState_FoldKeywordUp
				EndIf
		Next n
		For n=1 To CountString(KeyWordDown, " ") + 1
				If LCase(StringField(KeyWordDown, n, " ")) = LCase(key)
						ProcedureReturn #LexerState_FoldKeywordDown
				EndIf
		Next n
		For n=1 To CountString(KeyWordNone, " ") + 1
				If LCase(StringField(KeyWordNone, n, " ")) = LCase(key)
						ProcedureReturn #LexerState_Keyword
				EndIf
		Next n
		ProcedureReturn -1
EndProcedure


Procedure Highlight(sciptr.l, endpos.l)
		Protected level = #SC_FOLDLEVELBASE, Char.l, keyword.s, state.i, linenumber.l
		Protected thislevel.l = level
		Protected nextlevel.l = level
		Protected CurrentPos.l = 0, endlinepos.l, startkeyword
		Protected currentline.l = 0
		endpos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, endpos))
		ScintillaSendMessage(sciptr, #SCI_STARTSTYLING, CurrentPos, $1F | #INDICS_MASK)
		
		While CurrentPos <= endpos
				Char = ScintillaSendMessage(sciptr, #SCI_GETCHARAT, CurrentPos)
				Char2= ScintillaSendMessage(sciptr, #SCI_GETCHARAT, CurrentPos+1)
				Select Char
						Case 10
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, 1, #LexerState_NonKeyword)
						ScintillaSendMessage(sciptr, #SCI_SETFOLDLEVEL, linenumber , thislevel)
						thislevel = nextlevel
						linenumber + 1
						Case 'a' To 'z', 'A' To 'Z'
						endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
						
						keyword = Chr(char)
						While currentpos < endlinepos
								currentpos + 1
								char = ScintillaSendMessage(sciptr, #SCI_GETCHARAT, currentpos)
								If Not ((char >= 'a' And char <= 'z') Or (char >= 'A' And char <= 'Z') Or char = '_'Or (char >= '0' And char <= '9'))
										currentpos-1
										Break
								EndIf
								keyword + Chr(char)
						Wend
						Select KeyWordIs(keyword)
								Case #LexerState_FoldKeywordUp
								state = #LexerState_Keyword
								thislevel | #SC_FOLDLEVELHEADERFLAG
								nextlevel + 1
								Case #LexerState_FoldKeywordDown
								state = #LexerState_Keyword
								nextlevel - 1
								If nextlevel < #SC_FOLDLEVELBASE
										nextlevel = #SC_FOLDLEVELBASE
								EndIf
								Case #LexerState_Keyword
								state = #LexerState_Keyword
								Default
								
								state = #LexerState_NonKeyword
						EndSelect
						
						
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, Len(keyword), state)
						
						Case '{','}'
					
						endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
						startkeyword = 1
						While currentpos < endlinepos
								currentpos + 1
								startkeyword + 1
								If ScintillaSendMessage(sciptr, #SCI_GETCHARAT, currentpos) = '{' or ScintillaSendMessage(sciptr, #SCI_GETCHARAT, currentpos) = '}'
										Break
								EndIf
						Wend
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, startkeyword, 	#LexerState_braquet)
						Case '"'
						endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
						startkeyword = 1
						While currentpos < endlinepos
								currentpos + 1
								startkeyword + 1
								If ScintillaSendMessage(sciptr, #SCI_GETCHARAT, currentpos) = '"'
										Break
								EndIf
						Wend
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, startkeyword, #LexerState_String)
						Case '/' ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< By Dobro <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
					If char2 ='/'
				
						endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
						startkeyword = 1
						While currentpos < endlinepos
								currentpos + 1
								startkeyword + 1
						Wend
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, startkeyword, #LexerState_Comment)
						Else
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, 1, #LexerState_Space)
						Endif
						Case 9, ' '
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, 1, #LexerState_Space)
						Case '#'
						endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
						startkeyword = 1
						While currentpos < endlinepos
								currentpos + 1
								char = ScintillaSendMessage(sciptr, #SCI_GETCHARAT, currentpos)
								If Not ((char >= 'a' And char <= 'z') Or (char >= 'A' And char <= 'Z') Or char = '_' Or (char >= '0' And char <= '9'))
										currentpos-1
										Break
								EndIf
								startkeyword + 1
						Wend
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, startkeyword, #LexerState_Constant)
						Default
						ScintillaSendMessage(sciptr, #SCI_SETSTYLING, 1, #LexerState_NonKeyword)
				EndSelect
				currentpos+1
		Wend
EndProcedure

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
		Select *scinotify\nmhdr\code
				Case #SCN_STYLENEEDED
				Highlight(Gadget, *scinotify\position)
				Case #SCN_MARGINCLICK
				ScintillaSendMessage(Gadget, #SCI_TOGGLEFOLD, ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, *scinotify\Position))
		EndSelect
EndProcedure






; Epb




Image
Image
Site: http://michel.dobro.free.fr/
Devise :"dis moi ce dont tu as besoin, je t'expliquerai comment t'en passer"
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par falsam »

Merci Zorro. Ca fonctionne (YeahhHHhhhh) mais là je suis épaté !

Pour le moment la seule différence que je vois c'est que contrairement à moi, lors du processus de coloration syntaxique quand tu rencontre le caractère "/" tu regardes si le suivant est aussi un "/" alors que moi je cherche si le précédent est bien un "/". Et ça c'est en fait LA bonne façon de faire.

J'ai tenté de faire la même chose dans mon code et pas de chance même souci que dans mon code précédent.

:idea: J'ai enlevé de ton code tout ce qui ne sert pas et corrigé la coloration des bracket car la boucle de traitement est inutile.
Ton code ressemble maintenant au mien et fonctionne alors que le mien ne fonctionne toujours pas.

Voici ton code minimalisé et fonctionnel.

Code : Tout sélectionner

EnableExplicit

Enumeration Highlight
  #Style_Comment
  #Style_Bracket
  #Style_NonKeyword
EndEnumeration

Global Event, Quit

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  InitScintilla("Scintilla.dll")
CompilerEndIf

Declare scintillacallback(gadget, *scinotify.scnotification)
Declare ScintillaHighlight(Gadget.l, endpos.l)
Declare ScintillaGetLineEndPosition(gadget, line)
Declare ScintillaLineFromPosition(gadget, pos)


If OpenWindow(0, 0, 0, 800, 600, "Scintilla exemple", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)  
    ScintillaGadget(0, 0, 0, 800, 600, @ScintillaCallBack())
        
    ; Set default font
    ScintillaSendMessage(0, #SCI_STYLESETFONT, #STYLE_DEFAULT, @"Comic sans Ms")
    ScintillaSendMessage(0, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 12)
    ScintillaSendMessage(0, #SCI_STYLECLEARALL)
    
    ; Set caret line colour
    ScintillaSendMessage(0, #SCI_SETCARETLINEBACK, $eeeeff)
    ScintillaSendMessage(0, #SCI_SETCARETLINEVISIBLE, #True)
    
    ; Set styles for custom lexer
    ScintillaSendMessage(0, #SCI_STYLESETFORE, #Style_Comment, $bb00)
    ScintillaSendMessage(0, #SCI_STYLESETITALIC, #Style_Comment, 1)
    ScintillaSendMessage(0, #SCI_STYLESETFORE, #Style_NonKeyword, 0)
    ScintillaSendMessage(0, #SCI_STYLESETFORE,  #Style_Bracket, RGB(255, 0, 0))
    
    ; Margins
    ScintillaSendMessage(0, #SCI_SETMARGINTYPEN, 0, #SC_MARGIN_NUMBER)
    ScintillaSendMessage(0, #SCI_SETMARGINWIDTHN, 0, 50)
  
  Repeat 
    event = WaitWindowEvent()
    
    If event = #PB_Event_CloseWindow
      Quit = 1
    EndIf
  Until Quit = 1  
EndIf
End

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
  Select *scinotify\nmhdr\code
    Case #SCN_STYLENEEDED
      ScintillaHighlight(Gadget, *scinotify\position)
  EndSelect
EndProcedure

Procedure ScintillaHighlight(Gadget.l, endpos.l)
  Protected Char.l
  Protected CurrentPos.l = 0, endlinepos.l, startkeyword
  
  ScintillaSendMessage(Gadget, #SCI_STARTSTYLING, CurrentPos, $1F | #INDICS_MASK)
  
  While CurrentPos <= endpos
    Char = ScintillaSendMessage(Gadget, #SCI_GETCHARAT, CurrentPos)
    
    Select Char        
       Case '{','}'  
         ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 1, #Style_Bracket)
                 
      Case '/' ; By Dobro
        If ScintillaSendMessage(Gadget, #SCI_GETCHARAT, CurrentPos+1) ='/'
          endlinepos = ScintillaGetLineEndPosition(Gadget, ScintillaLineFromPosition(Gadget, currentpos))
          startkeyword = 1
          While currentpos < endlinepos
            currentpos + 1
            startkeyword + 1
          Wend
          ScintillaSendMessage(Gadget, #SCI_SETSTYLING, startkeyword,  #Style_Comment)
        Else
          ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 1,  #Style_NonKeyword)
        EndIf
        
      Default
        ScintillaSendMessage(Gadget, #SCI_SETSTYLING, 1,  #Style_NonKeyword)
    EndSelect
    currentpos+1
  Wend
EndProcedure

Procedure ScintillaGetLineEndPosition(gadget, line)
  ProcedureReturn ScintillaSendMessage(gadget,#SCI_GETLINEENDPOSITION, line)
EndProcedure

Procedure ScintillaLineFromPosition(gadget, Pos)
  ProcedureReturn ScintillaSendMessage(gadget,#SCI_LINEFROMPOSITION, Pos)
EndProcedure
Je me lance dans la comparaison avec mon code. Merci encore une fois Zorro.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par falsam »

Zorro ta solution est la bonne : Lors du processus de coloration syntaxique, quand le process repère le caractère "/" il faut vérifier que le caractère suivant est lui aussi un "/".

Code : Tout sélectionner

Case '/' ; By Dobro
        If ScintillaSendMessage(Gadget, #SCI_GETCHARAT, CurrentPos+1) ='/'
Dans mon code je faisais le contraire.

Code : Tout sélectionner

Case '/' ;Comment
        ;Is the preceding character a /?
        If ScintillaSendMessage(Gadget, #SCI_GETCHARAT, currentpos - 1) = '/'
Sujet résolu :wink:
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Zorro
Messages : 2185
Inscription : mar. 31/mai/2016 9:06

Re: ScintillaGadget : Colorer les commentaires d'un code JS

Message par Zorro »

bon et bien tant mieux :)
Image
Image
Site: http://michel.dobro.free.fr/
Devise :"dis moi ce dont tu as besoin, je t'expliquerai comment t'en passer"
Répondre