View unanswered posts | View active topics



Reply to topic  [ 10 posts ] 
Script: open file by text selection 
Author Message
Member

Joined: Wed Sep 26, 2007 9:33 am
Posts: 8
Reply with quote
Simply make a selection and run this script. I used macros to run it and nicely attached a keyboard shortcut. It will search for a file in your project's directory.
Actually my problem was that I had a lot of folders and files. For example:
/basic/wrapper/component/manager.php
and in the php code
$component = new basic_wrapper_component_manager();
So the script parses the selection and tries to split it by '_'. Lately I added '/' also. Of course you can skip this splitting part.

Code:
using System;
using System.Windows.Forms;
using System.Text;
using System.Text.RegularExpressions;
using ScintillaNet;
using PluginCore;
using PluginCore.Helpers;
using System.IO;
using System.Drawing;

public class FDScript
{

   private static string file;
   private static string projectPath;

    public static void Execute() {
      
      file = PluginBase.MainForm.CurrentDocument.SciControl.SelText;
      projectPath = Path.GetDirectoryName(PluginBase.CurrentProject.ProjectPath);
      
      if(file == "") {
         InputBox("File name", "Type a file name:", ref file, "Ok", "Cancel");
      }
      if(file != "") {
         char[] delimiterChars = {'_', '/'};
         foreach( char c in delimiterChars) {
            searchForDelimeters(c);
         }
         // alert(projectPath + "  /  " + file);
         if(!findFile(projectPath, file)) {
            alert("File (" + file + ") not found.");
         }
      }
    }
   public static void searchForDelimeters(char delimeter){
      string projectPathAddition = "";
      if(file.IndexOf(delimeter) >= 0) {
         string[] pathParts = file.Split(new Char [] {delimeter});
         int numOfParts = pathParts.Length;
         if(numOfParts > 1) {
            for (int i=0; i<numOfParts-1; i++) {
               projectPathAddition += pathParts[i] + "\\";
            }
            if(InputBox("Found delimeter '" + delimeter + "'", "Found delimeter '" + delimeter + "'. Search for '" + pathParts[numOfParts-1] + "' in:", ref projectPathAddition, "Yes", "No") == DialogResult.OK) {
               projectPath += "\\" + projectPathAddition;
               file = pathParts[numOfParts-1];
            }
         }
      }
   }
   public static bool findFile(string dir, string file) {
      bool result = false;
      try   {
         foreach (string f in Directory.GetFiles(dir, file + ".?*")) {
            PluginCore.PluginBase.MainForm.OpenEditableDocument(f, true);
            result = true;
         }
         foreach (string d in Directory.GetDirectories(dir)) {
            if(findFile(d, file)) {
               result = true;
            }
         }
      } catch (System.Exception excpt) {
         alert(excpt.Message);
      }
      return result;
   }
   public static void alert(string mess) {
       MessageBox.Show(mess);
   }
   public static DialogResult InputBox(string title, string promptText, ref string value, string labelOk, string labelCancel) {
      Form form = new Form();
      Label label = new Label();
      TextBox textBox = new TextBox();
      Button buttonOk = new Button();
      Button buttonCancel = new Button();
      
      form.Text = title;
      label.Text = promptText;
      textBox.Text = value;
      
      buttonOk.Text = labelOk;
      buttonCancel.Text = labelCancel;
      buttonOk.DialogResult = DialogResult.OK;
      buttonCancel.DialogResult = DialogResult.Cancel;
      
      label.SetBounds(9, 20, 372, 13);
      textBox.SetBounds(12, 36, 372, 20);
      buttonOk.SetBounds(228, 72, 75, 23);
      buttonCancel.SetBounds(309, 72, 75, 23);
      
      label.AutoSize = true;
      textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
      buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
      buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
      
      form.ClientSize = new Size(396, 107);
      form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
      form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
      form.FormBorderStyle = FormBorderStyle.FixedDialog;
      form.StartPosition = FormStartPosition.CenterScreen;
      form.MinimizeBox = false;
      form.MaximizeBox = false;
      form.AcceptButton = buttonOk;
      form.CancelButton = buttonCancel;
      
      DialogResult dialogResult = form.ShowDialog();
      value = textBox.Text;
      return dialogResult;
   }

}

more about the script here http://krasimirtsonev.com/blog/article/ ... ros-basics

_________________
http://krasimirtsonev.com/blog


Mon Mar 21, 2011 1:53 pm
Profile WWW
Admin

Joined: Tue Aug 30, 2005 6:14 pm
Posts: 3046
Location: Finland
Reply with quote
Nice work and nice to see people using FD's powerful hacking features. Thanks for sharing.


Mon Mar 21, 2011 4:20 pm
Profile WWW
Member

Joined: Wed Sep 26, 2007 9:33 am
Posts: 8
Reply with quote
Actually because of this feature (running of C# scripts) we can build what ever we need. There is no limits :)

_________________
http://krasimirtsonev.com/blog


Mon Mar 21, 2011 4:23 pm
Profile WWW
Admin

Joined: Tue Aug 30, 2005 6:14 pm
Posts: 3046
Location: Finland
Reply with quote
That was pretty much the idea behind it. :)


Mon Mar 21, 2011 4:58 pm
Profile WWW
Member

Joined: Wed Sep 26, 2007 9:33 am
Posts: 8
Reply with quote
Hello, after a couple of days using my script I decided to rewrite it and convert it to something like fast file explorer. It looks like that:

Image

Download the source from here:
http://krasimirtsonev.com/files/fdmacros/OpenFileV2.cs

_________________
http://krasimirtsonev.com/blog


Tue Mar 29, 2011 5:46 pm
Profile WWW
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 12172
Location: London
Reply with quote
You've got that already in FD4 under Ctrl+R ;)


Tue Mar 29, 2011 6:37 pm
Profile WWW
Member

Joined: Wed Sep 26, 2007 9:33 am
Posts: 8
Reply with quote
Ah ... I'm still using FD 3.3.2 but have something similar when I use Ctrl + R. Unfortunately this feature currently lags a lot and doesn't use my current document text selection. Also it doesn't work very well with big projects (+5000 files and more). Not sure, maybe it's something with my copy.

_________________
http://krasimirtsonev.com/blog


Tue Mar 29, 2011 6:50 pm
Profile WWW
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 12172
Location: London
Reply with quote
Right it was in FD3 already, only a bit improved in FD4.

Only that is arguably slow is exploring the filesystem but it works just like your script (it needs some caching).


Tue Mar 29, 2011 7:34 pm
Profile WWW
Member

Joined: Wed Sep 26, 2007 9:33 am
Posts: 8
Reply with quote
Thanks. Didn't know about that actually. Will try it for sure :)

_________________
http://krasimirtsonev.com/blog


Tue Mar 29, 2011 7:37 pm
Profile WWW
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 12172
Location: London
Reply with quote
That said Ctrl+R doesn't use the selected text - but it would be easy enough to add.


Tue Mar 29, 2011 9:56 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 10 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.