delphidabbler / codesnip

A code bank designed with Pascal in mind
https://delphidabbler.com/software/codesnip
Other
110 stars 33 forks source link

if unit need .inc file, How can I do? #123

Closed YXGust closed 8 months ago

YXGust commented 8 months ago

like this:

unit XSuperObject;

interface

{$I XSuperObject.inc}

uses
  Classes,
  Variants,
  SysUtils,
  Character,
  XSuperJSON,

how can i add file "XSuperObject.inc" in code snippet

delphidabbler commented 8 months ago

I'm assuming you want to test compile a function or class that needs an include file?

I'd never considered the need to do this. For a simple function/procedure I can't think of a way to do this. This is because of assumptions about code format that CodeSnip makes means that the inc file gets included twice so the following won't work:

{$I Test.inc}

function XXX: Integer;
begin
  // do something with content of Test.inc
end;

But, if you create a class, you can do it. For example if you have a an include file Test.inc in C:\Code\Tests, here's what to do:

  1. Go to the Tools | Configure Compilers menu. In the dialogue box, select the compiler you want to use for test compiling. Select the Search Paths tab and enter C:\Code\Tests in the edit box and click Add. You need to do this for each compiler you use. Then click OK to close the dialogue box.

  2. Create C:\Code\Tests\Test.inc, containing the line:

    const IncTest = 42;
  3. Create a new snippet (Ctrl+Ins).

  4. Enter the required name and description.

  5. Set Kind to Class / Advanced Record.

  6. Enter the following source code for the snippet:

    type
     TC = class
     public
       function test: Integer;
     end;
    
    {$I Test.inc}
    
    function TC.test: Integer;
    begin
     Result := IncTest;
    end;
  7. Hit F9 to test compile the snippet. It should work! I have Delphu XE, 11 & 12 installed but only configured Delphi 12 with C:\Code\Tests in the search path. Here's my result, showing compile failures with XE and 11 and success with 12:

CodeSnip-20231230-0544_480x350

If you would like me to consider adding a facility to compile include files for all compilable snippet types, please open a new issue and ask for it.

Hope this helps.

YXGust commented 8 months ago

Yes,we can use "Search Paths". Thank you, with your guidance, I succeeded.

delphidabbler commented 8 months ago

Glad it worked. Cheers