Skip to content

Commit

Permalink
Cython support
Browse files Browse the repository at this point in the history
  • Loading branch information
pyscripter committed Mar 5, 2012
1 parent a8843dc commit f1d5474
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 90 deletions.
9 changes: 6 additions & 3 deletions PyScripter.dproj
Expand Up @@ -100,11 +100,13 @@
<VerInfo_Locale>1033</VerInfo_Locale>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2_Win32)'!=''">
<VerInfo_Release>0</VerInfo_Release>
<VerInfo_MinorVer>5</VerInfo_MinorVer>
<ILINK_FullDebugInfo>true</ILINK_FullDebugInfo>
<BCC_SourceDebuggingOn>true</BCC_SourceDebuggingOn>
<BCC_DebugLineNumbers>true</BCC_DebugLineNumbers>
<Manifest_File>None</Manifest_File>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=2.4.4.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=2.5.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<VerInfo_Locale>1033</VerInfo_Locale>
</PropertyGroup>
<ItemGroup>
Expand Down Expand Up @@ -312,7 +314,7 @@
<CfgParent>Base</CfgParent>
</BuildConfiguration>
</ItemGroup>
<Import Project="$(BDS)\Bin\CodeGear.Delphi.Targets" Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')"/>
<Import Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')" Project="$(BDS)\Bin\CodeGear.Delphi.Targets"/>
<ProjectExtensions>
<Borland.Personality>Delphi.Personality.12</Borland.Personality>
<Borland.ProjectType>VCLApplication</Borland.ProjectType>
Expand Down Expand Up @@ -354,6 +356,7 @@
<VersionInfoKeys Name="Comments"/>
</VersionInfoKeys>
<Excluded_Packages>
<Excluded_Packages Name="C:\Users\Public\Documents\RAD Studio\9.0\Bpl\PythonVCL_XE2.bpl">VCL Components for Python</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k160.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dclofficexp160.bpl">Microsoft Office XP Sample Automation Server Wrapper Components</Excluded_Packages>
</Excluded_Packages>
Expand All @@ -366,5 +369,5 @@
</BorlandProject>
<ProjectFileVersion>12</ProjectFileVersion>
</ProjectExtensions>
<Import Project="$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj" Condition="Exists('$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj')"/>
<Import Condition="Exists('$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj')" Project="$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj"/>
</Project>
Binary file modified PyScripter.res
Binary file not shown.
1 change: 1 addition & 0 deletions StringResources.pas
Expand Up @@ -227,6 +227,7 @@ interface
SFileTemplateCategoryInternet = 'Internet';
SFileTemplateCategoryOther = 'Other';
SPythonTemplateName = 'Python Script';
SCythonTemplateName = 'Cython Script';
SCSSFileTemplateName = 'Cascading Style Sheet';
SHTMLFileTemplateName = 'HTML Document';
STextFileTemplateName = 'Text File';
Expand Down
106 changes: 100 additions & 6 deletions SynHighlighterPython.pas
Expand Up @@ -116,7 +116,7 @@ TSynPythonSyn = class(TSynCustomHighLighter)
protected
function GetSampleSource: UnicodeString; override;
function IsFilterStored: Boolean; override;
function GetKeywordIdentifiers: TStringList;
function GetKeywordIdentifiers: TStringList; virtual;
property TokenID: TtkTokenKind read FTokenID;
procedure DispatchProc; virtual;
public
Expand Down Expand Up @@ -221,14 +221,22 @@ TSynPythonInterpreterSyn = class(TSynPythonSyn)
property PM : string read fPM write fPM;
end;

TSynCythonSyn = class(TSynPythonSyn)
protected
function GetKeywordIdentifiers: TStringList; override;
public
constructor Create(AOwner: TComponent); override;
procedure AddCythonKeywords(KeywordList: TStringList);
class function GetLanguageName: string; override;
class function GetFriendlyLanguageName: UnicodeString; override;
end;



var
PythonGlobalKeywords: TStringList;
CythonGlobalKeywords: TStringList;

implementation

uses
StrUtils,
SynEditStrConst;

resourcestring
SYNS_CommentedCode = 'Commented Code';
Expand All @@ -243,6 +251,13 @@ implementation
SYNS_FriendlyMatchingBrace = 'Matching Brace';
SYNS_FriendlyUnbalancedBrace = 'Unbalanced Brace';
SYNS_FriendlyMultiLineString = 'Multi-Line String';
SYNS_FilterCython = 'Cython Files (*.pyx*.pxd;*.pxi)|*.pyx;*.pxd;*.pxi';

implementation

uses
StrUtils,
SynEditStrConst;

function TSynPythonSyn.GetKeyWords(TokenKind: Integer): UnicodeString;
begin
Expand Down Expand Up @@ -1421,6 +1436,8 @@ class function TSynPythonSyn.GetFriendlyLanguageName: UnicodeString;
SYNS_FriendlyAttrPrompt = 'Prompt';
SYNS_LangPythonInterpreter = 'Python Interpreter';
SYNS_FriendlyLangPythonInterpreter = 'Python Interpreter';
SYNS_LangCython = 'Cython';
SYNS_FriendlyLangCython = 'Cython';

procedure TSynPythonInterpreterSyn.BannerProc;
begin
Expand Down Expand Up @@ -1565,11 +1582,88 @@ procedure TSynPythonInterpreterSyn.TracebackProc;
inc(Run);
end;

{ TSynCythonSyn }

constructor TSynCythonSyn.Create(AOwner: TComponent);
begin
inherited;
fDefaultFilter := SYNS_FilterCython;
end;

class function TSynCythonSyn.GetFriendlyLanguageName: UnicodeString;
begin
Result := SYNS_FriendlyLangCython;
end;

procedure TSynCythonSyn.AddCythonKeywords(KeywordList: TStringList);
const
// No need to localise keywords!

// List of keywords
KEYWORDCOUNT = 7;
KEYWORDS: array [1..KEYWORDCOUNT] of UnicodeString =
(
'cdef',
'ctypedef',
'cpdef',
'inline',
'cimport',
'include',
'DEF'
);

// List of non-keyword identifiers
NONKEYWORDCOUNT = 13;
NONKEYWORDS: array [1..NONKEYWORDCOUNT] of UnicodeString =
(
'bool',
'char',
'double',
'enum',
'float',
'int',
'long',
'mutable',
'short',
'signed',
'struct',
'unsigned',
'void'
);
var
f: Integer;
begin
for f := 1 to KEYWORDCOUNT do
KeywordList.AddObject(KEYWORDS[f], Pointer(Ord(tkKey)));
for f := 1 to NONKEYWORDCOUNT do
KeywordList.AddObject(NONKEYWORDS[f], Pointer(Ord(tkNonKeyword)));
end;

function TSynCythonSyn.GetKeywordIdentifiers: TStringList;
begin
inherited;
if not Assigned (CythonGlobalKeywords) then
begin
// Create the string list of keywords - only once
CythonGlobalKeywords := TStringList.Create;
CythonGlobalKeywords.AddStrings(PythonGlobalKeywords);
AddCythonKeywords(CythonGlobalKeywords);
end; // if
Result := CythonGlobalKeywords;
end;

class function TSynCythonSyn.GetLanguageName: string;
begin
Result := SYNS_LangCython;
end;

initialization
{$IFNDEF SYN_CPPB_1}
RegisterPlaceableHighlighter(TSynPythonSyn);
RegisterPlaceableHighlighter(TSynCythonSyn);
{$ENDIF}
finalization
PythonGlobalKeywords.Free;
CythonGlobalKeywords.Free;
end.

17 changes: 16 additions & 1 deletion cFileTemplates.pas
Expand Up @@ -32,7 +32,8 @@ TFileTemplate = class(TInterfacedPersistent, IJvAppStorageHandler)
TFileTemplates = class(TObjectList)
function CreateListItem(Sender: TJvCustomAppStorage; const Path: string;
Index: Integer): TPersistent;
procedure AddPythonTemplate;
procedure AddPythonTemplate;
procedure AddCythonTemplate;
procedure AddHTMLTemplate;
procedure AddCSSTemplate;
procedure AddXMLTemplate;
Expand Down Expand Up @@ -116,6 +117,19 @@ procedure TFileTemplates.AddCSSTemplate;
Add(FileTemplate);
end;

procedure TFileTemplates.AddCythonTemplate;
Var
FileTemplate : TFileTemplate;
begin
FileTemplate := TFileTemplate.Create;
FileTemplate.Name := _(SCythonTemplateName);
FileTemplate.Extension := 'pyx';
FileTemplate.Category := 'Python';
FileTemplate.Highlighter := 'Cython';
FileTemplate.Template := _(SPythonFileTemplate);
Add(FileTemplate);
end;

procedure TFileTemplates.AddHTMLTemplate;
Var
FileTemplate : TFileTemplate;
Expand Down Expand Up @@ -229,6 +243,7 @@ initialization
FileTemplates := TFileTemplates.Create(True);
with FileTemplates do begin
AddPythonTemplate;
AddCythonTemplate;
AddHTMLTemplate;
AddXMLTemplate;
AddCSSTemplate;
Expand Down
4 changes: 2 additions & 2 deletions dmCommands.dfm
Expand Up @@ -70,7 +70,7 @@ object CommandsDataModule: TCommandsDataModule
Left = 32
Top = 241
Bitmap = {
494C01010A003000B40010001000FFFFFFFF2110FFFFFFFFFFFFFFFF424D3600
494C01010A003000B80010001000FFFFFFFF2110FFFFFFFFFFFFFFFF424D3600
0000000000003600000028000000400000003000000001002000000000000030
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
Expand Down Expand Up @@ -4373,7 +4373,7 @@ object CommandsDataModule: TCommandsDataModule
Left = 36
Top = 194
Bitmap = {
494C01019800B400140110001000FFFFFFFF2110FFFFFFFFFFFFFFFF424D3600
494C01019800B400180110001000FFFFFFFF2110FFFFFFFFFFFFFFFF424D3600
0000000000003600000028000000400000007002000001002000000000000070
0200000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
Expand Down
37 changes: 27 additions & 10 deletions dmCommands.pas
Expand Up @@ -41,6 +41,7 @@ TPythonIDEOptions = class(TBaseOptions)
fExporerInitiallyExpanded : Boolean;
fSearchTextAtCaret : Boolean;
fPythonFileFilter : string;
fCythonFileFilter : string;
fHTMLFileFilter : string;
fXMLFileFilter : string;
fCSSFileFilter : string;
Expand Down Expand Up @@ -120,6 +121,8 @@ TPythonIDEOptions = class(TBaseOptions)
write fSearchTextAtCaret stored False;
property PythonFileFilter : string read fPythonFileFilter
write fPythonFileFilter;
property CythonFileFilter : string read fCythonFileFilter
write fCythonFileFilter;
property HTMLFileFilter : string read fHTMLFileFilter
write fHTMLFileFilter;
property XMLFileFilter : string read fXMLFileFilter
Expand Down Expand Up @@ -219,8 +222,6 @@ TPythonIDEOptions = class(TBaseOptions)
write fCompleteWithWordBreakChars;
property CompleteWithOneEntry : Boolean read fCompleteWithOneEntry
write fCompleteWithOneEntry;


end;
{$METHODINFO OFF}

Expand Down Expand Up @@ -477,6 +478,7 @@ TCommandsDataModule = class(TDataModule)
fConfirmReplaceDialogRect: TRect;
public
SynYAMLSyn: TSynYAMLSyn;
SynCythonSyn: TSynCythonSyn;
BlockOpenerRE : TRegExpr;
BlockCloserRE : TRegExpr;
CommentLineRE : TRegExpr;
Expand Down Expand Up @@ -603,6 +605,7 @@ procedure TPythonIDEOptions.Assign(Source: TPersistent);
Self.fExporerInitiallyExpanded := ExporerInitiallyExpanded;
Self.fSearchTextAtCaret := SearchTextAtCaret;
Self.fPythonFileFilter := PythonFileFilter;
Self.fCythonFileFilter := CythonFileFilter;
Self.fHTMLFileFilter := HTMLFileFilter;
Self.fXMLFileFilter := XMLFileFilter;
Self.fCSSFileFilter := CSSFileFilter;
Expand Down Expand Up @@ -683,6 +686,7 @@ constructor TPythonIDEOptions.Create;
fCreateBackupFiles := False;
fExporerInitiallyExpanded := False;
fPythonFileFilter := 'Python Files (*.py;*.pyw)|*.py;*.pyw';
fCythonFileFilter := SYNS_FilterCython;
fHTMLFileFilter := SYNS_FilterHTML;
fXMLFileFilter := SYNS_FilterXML;
fCSSFileFilter := SYNS_FilterCSS;
Expand Down Expand Up @@ -913,8 +917,14 @@ procedure TCommandsDataModule.DataModuleCreate(Sender: TObject);
except
end;

// DefaultOptions
PyIDEOptions := TPythonIDEOptions.Create;

// Setup Highlighters
SynYAMLSyn := TSynYAMLSyn.Create(Self);
SynCythonSyn := TSynCythonSyn.Create(Self);
SynCythonSyn.Assign(SynPythonSyn);
SynCythonSyn.DefaultFilter := PyIDEOptions.CythonFileFilter;
fHighlighters := TStringList.Create;
TStringList(fHighlighters).CaseSensitive := False;
GetHighlighters(Self, fHighlighters, False);
Expand All @@ -924,6 +934,10 @@ procedure TCommandsDataModule.DataModuleCreate(Sender: TObject);
Index := fHighlighters.IndexOf(SynPythonSyn.FriendlyLanguageName);
if Index >= 0 then fHighlighters.Delete(Index);
fHighlighters.InsertObject(0, SynPythonSyn.FriendlyLanguageName, SynPythonSyn);
// Place Cython last
Index := fHighlighters.IndexOf(SynCythonSyn.FriendlyLanguageName);
if Index >= 0 then fHighlighters.Delete(Index);
fHighlighters.AddObject(SynCythonSyn.FriendlyLanguageName, SynCythonSyn);

// this is to save the internal state of highlighter attributes
// Work around for the reported bug according to which some
Expand All @@ -934,9 +948,6 @@ procedure TCommandsDataModule.DataModuleCreate(Sender: TObject);
SetAttributesOnChange(DefHighlightChange);
end;

// DefaultOptions
PyIDEOptions := TPythonIDEOptions.Create;

// SynWeb Highlighters do not provide default filters
SynWebHTMLSyn.DefaultFilter := PyIDEOptions.HTMLFileFilter;
SynWebXMLSyn.DefaultFilter := PyIDEOptions.XMLFileFilter;
Expand Down Expand Up @@ -1058,7 +1069,7 @@ function TCommandsDataModule.GetHighlighterForFile(
procedure TCommandsDataModule.SynEditOptionsDialogGetHighlighterCount(Sender: TObject;
var Count: Integer);
begin
Count := fHighlighters.Count;
Count := fHighlighters.Count - 1; // Cython gets Python highlighting
end;

procedure TCommandsDataModule.SynEditOptionsDialogGetHighlighter(Sender: TObject;
Expand Down Expand Up @@ -1462,7 +1473,9 @@ procedure TCommandsDataModule.ApplyEditorOptions;
InterpreterEditorOptions.Keystrokes.Assign(EditorOptions.Keystrokes);
PythonIIForm.SynEdit.Keystrokes.Assign(EditorOptions.Keystrokes);
PythonIIForm.RegisterHistoryCommands;
PythonIIForm.SynEdit.Highlighter.Assign(CommandsDataModule.SynPythonSyn);
PythonIIForm.SynEdit.Highlighter.Assign(SynPythonSyn);
SynCythonSyn.Assign(SynPythonSyn);
SynCythonSyn.DefaultFilter := PyIDEOptions.CythonFileFilter;
end;

procedure TCommandsDataModule.actEditorOptionsExecute(Sender: TObject);
Expand Down Expand Up @@ -1765,7 +1778,9 @@ procedure TCommandsDataModule.actImportHighlightersExecute(Sender: TObject);
for i := 0 to Highlighters.Count - 1 do
AppStorage.ReadPersistent('Highlighters\'+Highlighters[i],
TPersistent(Highlighters.Objects[i]));
PythonIIForm.SynEdit.Highlighter.Assign(CommandsDataModule.SynPythonSyn);
PythonIIForm.SynEdit.Highlighter.Assign(SynPythonSyn);
SynCythonSyn.Assign(SynPythonSyn);
SynCythonSyn.DefaultFilter := PyIDEOptions.CythonFileFilter;
if AppStorage.IniFile.SectionExists('Highlighters\Intepreter') then
AppStorage.ReadPersistent('Highlighters\Intepreter',
PythonIIForm.SynEdit.Highlighter);
Expand Down Expand Up @@ -2223,7 +2238,7 @@ procedure TCommandsDataModule.actIDEOptionsExecute(Sender: TObject);
end;
with Categories[3] do begin
DisplayName := _('File Filters');
SetLength(Options, 9);
SetLength(Options, 10);
Options[0].PropertyName := 'PythonFileFilter';
Options[0].DisplayName := _('Open dialog Python filter');
Options[1].PropertyName := 'HTMLFileFilter';
Expand All @@ -2242,6 +2257,8 @@ procedure TCommandsDataModule.actIDEOptionsExecute(Sender: TObject);
Options[7].DisplayName := _('Open dialog PHP filter');
Options[8].PropertyName := 'FileExplorerFilter';
Options[8].DisplayName := _('File explorer filter');
Options[9].PropertyName := 'CythonFileFilter';
Options[9].DisplayName := _('Open dialog Cython filter');
end;
with Categories[4] do begin
DisplayName := _('Editor');
Expand Down Expand Up @@ -3139,7 +3156,7 @@ function TCommandsDataModule.DoSearchReplaceText(SynEdit : TSynEdit;

function TCommandsDataModule.FileIsPythonSource(FileName: string): Boolean;
begin
Result := GetHighlighterForFile(FileName) = SynPythonSyn;
Result := GetHighlighterForFile(FileName) is TSynPythonSyn;
end;

function TCommandsDataModule.FindSearchTarget: ISearchCommands;
Expand Down

0 comments on commit f1d5474

Please sign in to comment.