0

I would like to create a building system in order to compile haskell files; note that I don't want to replace the common "CTRL+B" shortcut which runs the current file in a ST window.

so, following this messages, I created this file, located in the directory "/opt/sublime_text":

import sublime, sublime_plugin

class MakeHaskellCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      self.view.window().run_command('exec', {"working_dir":"${project_path:${folder}}",'cmd': ["ghc","$file"]})

then, I modified the user key binding of the sublime-haskell package:

[
    {
        "keys": ["f1"],
        "context": [
            { "key": "haskell_source" },
            { "key": "scanned_source" } ],
        "command": "MakeHaskellCommand"
    }
]

but after a restart of ST, nothing happens as I hit FN+F1.

can you help me?

EDIT thanks for your first message! It works, but now I have an other problem : I would like to delete all files in the directory, except the source file and the binary. I can launch this plugin:

import sublime
import sublime_plugin


class MakeHaskell2Command(sublime_plugin.WindowCommand):
    def run(self):
        variables = self.window.extract_variables()
        args = sublime.expand_variables({
            "working_dir": "${project_path:${file_path}}",
            "cmd": ["rm", "*.hi"],
            "cmd": ["rm", "*.o"]
        }, variables)
        self.window.run_command('exec', args)

but it does not remove the files. can you help me again for this?

Community
  • 1
  • 1
lolveley
  • 1,659
  • 2
  • 18
  • 34

1 Answers1

1

Just a few points:

  1. You create plugins in subfolders of Packages, e.g. the User folder
  2. The command names in the keymap are snake_case with the ending Command stripped of
  3. You should use a WindowCommand instead of a TextCommand for build systems
  4. You should press f1 not fn+f1
  5. The exec command does not expand the variables

To create your behavior press Tools >>> New Plugin..., paste and save:

import sublime
import sublime_plugin


class MakeHaskellCommand(sublime_plugin.WindowCommand):
    def run(self):
        variables = self.window.extract_variables()
        args = sublime.expand_variables({
            "working_dir": "${project_path:${file_path}}",
            "cmd": ["ghc", "$file"]
        }, variables)
        self.window.run_command('exec', args)

Afterwards open your keymap and insert the keybinding:

{
    "keys": ["f1"],
    "command": "make_haskell",
    "context":
    [
        { "key": "selector", "operator": "equal", "operand": "source.haskell" }
    ]
},

Edit: If you want do cleanup afterwards using the shell rm command, then you should use the shell_cmd instead of cmd. (exec assumes the shell_cmd to be a string and cmd to an array (why)) I slightly modified the plugin to cleanup afterwards:

import sublime
import sublime_plugin


class MakeHaskellCommand(sublime_plugin.WindowCommand):
    def run(self):
        variables = self.window.extract_variables()
        args = sublime.expand_variables({
            "working_dir": "$file_path",
            "shell_cmd": "ghc $file && rm $file_base_name.o $file_base_name.hi"
        }, variables)
        self.window.run_command('exec', args)
Community
  • 1
  • 1
r-stein
  • 4,627
  • 2
  • 16
  • 27