Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd popup on Sublime Text WIndows 10 + WSL #42

Closed
faustinoaq opened this issue Apr 8, 2018 · 18 comments
Closed

cmd popup on Sublime Text WIndows 10 + WSL #42

faustinoaq opened this issue Apr 8, 2018 · 18 comments
Labels

Comments

@faustinoaq
Copy link
Member

faustinoaq commented Apr 8, 2018

ezgif-1-e9165bf71d

This issue was discovered by @girng

Solution proposed by @oprypin :

  1. We should replace this line:

with subprocess.Popen([settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"], stdin = subprocess.PIPE, stdout = subprocess.PIPE) as proc:

  1. by something like this:
            if operating_system == 'win':
                # Prevent flashing windows if used from a GUI application
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                rc = subprocess.call(proc, stdout = pipe, stderr = pipe,
                                     startupinfo = startupinfo)

https://github.com/coin-or/pulp/blob/bac6d9d2214ba773d638d2de5149940cfd711359/src/pulp/solvers.py#L378-L382

@faustinoaq faustinoaq added the bug label Apr 8, 2018
@faustinoaq faustinoaq added this to To do in Crystal Tools via automation Apr 8, 2018
@ghost
Copy link

ghost commented Apr 8, 2018

thank you my friend for posting this

@ghost
Copy link

ghost commented Apr 19, 2018

hi, thx for support again.

i got
NameError: global name 'pipe' is not defined
so i changed the pipe to subprocess.PIPE, error went away.
but now, am getting:
NameError: global name 'proc' is not defined on line 33

this is line 33:
proc.stdin.write(bytes(src, 'UTF-8'))

@faustinoaq
Copy link
Member Author

@girng Looks like you should use pipe = open(os.devnull, 'w') instead, see:

https://github.com/coin-or/pulp/blob/bac6d9d2214ba773d638d2de5149940cfd711359/src/pulp/solvers.py#L376

@ghost
Copy link

ghost commented Apr 19, 2018

thank you, @faustinoaq did that. got rid of the pipe error, now still get:

NameError: global name 'proc' is not defined line 38, in run

line 38:
proc.stdin.write(bytes(src, 'UTF-8'))

@faustinoaq
Copy link
Member Author

@girng Try proc = [settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"]

@ghost
Copy link

ghost commented Apr 20, 2018

@faustinoaq ty again sir.. sorry for all this trouble.. lol

that error went away, but now getting:
AttributeError: 'list' object has no attribute 'stdin' in line 39:

proc.stdin.write(bytes(src, 'UTF-8'))

@ghost
Copy link

ghost commented Apr 20, 2018

top part of def run:

  def run(self, edit):
    vsize = self.view.size()
    region = sublime.Region(0, vsize)
    src = self.view.substr(region)
    window = self.view.window()

    settings = sublime.load_settings('Crystal.sublime-settings')
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    pipe = open(os.devnull, 'w')
    # Prevent flashing windows if used from a GUI application
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    proc = [settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"]
    rc = subprocess.call([settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"], stdout = pipe, stderr = pipe,startupinfo = startupinfo)
    proc.stdin.write(bytes(src, 'UTF-8'))
    proc.stdin.close()
    output = proc.stdout.read().decode('UTF-8')
    exit = proc.wait()
    pos = 0

@faustinoaq
Copy link
Member Author

faustinoaq commented Apr 20, 2018

@girng no problem 😅

Try this:

  def run(self, edit):
    vsize = self.view.size()
    region = sublime.Region(0, vsize)
    src = self.view.substr(region)
    window = self.view.window()

    settings = sublime.load_settings('Crystal.sublime-settings')
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    pipe = open(os.devnull, 'w')
    # Prevent flashing windows if used from a GUI application
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    cmd = [settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"]
    proc = subprocess.call(cmd, stdout = pipe, stderr = pipe,startupinfo = startupinfo)
    proc.stdin.write(bytes(src, 'UTF-8'))
    proc.stdin.close()
    output = proc.stdout.read().decode('UTF-8')
    exit = proc.wait()
    pos = 0
    # more code...

or this:

def run(self, edit):
  vsize = self.view.size()
  region = sublime.Region(0, vsize)
  src = self.view.substr(region)
  window = self.view.window()

  settings = sublime.load_settings('Crystal.sublime-settings')
  startupinfo = subprocess.STARTUPINFO()
  startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  pipe = open(os.devnull, 'w')
  # Prevent flashing windows if used from a GUI application
  startupinfo = subprocess.STARTUPINFO()
  startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  cmd = [settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"]
  with subprocess.call(cmd, stdout = pipe, stderr = pipe,startupinfo = startupinfo) as proc:
    proc.stdin.write(bytes(src, 'UTF-8'))
    proc.stdin.close()
    output = proc.stdout.read().decode('UTF-8')
    exit = proc.wait()
  pos = 0
  # more code...

@ghost
Copy link

ghost commented Apr 20, 2018

ty for continuing to help. this is painful xD

first code u supplied, i get:

AttributeError: 'int' object has no attribute 'stdin'
line 39:
proc.stdin.write(bytes(src, 'UTF-8'))


2nd code u supplied, i get:

    return self.run(edit)
  File "crystal_format in C:\Users\Administrator\AppData\Roaming\Sublime Text 3\Installed Packages\Crystal.sublime-package", line 38, in run
AttributeError: __exit__

line 38:
with subprocess.call(cmd, stdout = pipe, stderr = pipe,startupinfo = startupinfo) as proc:

@oprypin
Copy link
Contributor

oprypin commented Apr 20, 2018

What a mess...
Please try #43

@faustinoaq
Copy link
Member Author

@oprypin Thank you! 😅

@ghost
Copy link

ghost commented Apr 21, 2018

File "crystal_format in C:\Users\Administrator\AppData\Roaming\Sublime Text 3\Installed Packages\Crystal.sublime-package", line 45, in run
NameError: global name 'exit' is not defined

line 45:
if exit == 0:

@ghost
Copy link

ghost commented Apr 21, 2018

ok, so i commented out the if exit == 0 block (got rid of that error). however, it still doesn't format the code :(

sorry for all the trouble, im just trying reporting back if errors are found

@ghost
Copy link

ghost commented Apr 21, 2018

ok. got it working

  def run(self, edit):
    vsize = self.view.size()
    region = sublime.Region(0, vsize)
    src = self.view.substr(region)
    window = self.view.window()

    settings = sublime.load_settings('Crystal.sublime-settings')
    command = [settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"]

    popen_args = dict(args=command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    # Prevent flashing terminal windows
    if sys.platform.startswith('win'):
      popen_args['startupinfo'] = subprocess.STARTUPINFO()
      popen_args['startupinfo'].dwFlags |= subprocess.STARTF_USESHOWWINDOW

    with subprocess.Popen(**popen_args) as proc:
      output, _ = proc.communicate(src.encode('utf-8'))
      output = output.decode('utf-8')

    pos = 0
    if not self.has_redo():
      for op, text in diff_match_patch().diff_main(src, output):
        if op == diff_match_patch.DIFF_DELETE:
          self.view.erase(edit, sublime.Region(pos, pos + len(text)))
        if op == diff_match_patch.DIFF_INSERT:
          self.view.insert(edit, pos, text)
          pos += len(text)
        if op == diff_match_patch.DIFF_EQUAL:
          pos += len(text)

      self.view.erase_regions('crystal_errors')
      window.run_command("hide_panel")

    else:
      error = json.loads(output)
      error_pos = self.view.text_point(error[0]["line"] - 1, error[0]["column"] - 1)
      line_region = self.view.full_line(error_pos)
      self.view.add_regions('crystal_errors', [line_region], 'comment', 'dot', sublime.HIDDEN)

      error_panel = window.create_output_panel('crystal_errors')
      error_panel.run_command("append", {"characters":
        "Error at line %d, column %d: %s" % (error[0]["line"], error[0]["column"], error[0]['message'])
      })
      window.run_command("show_panel", {"panel": "output.crystal_errors"})

i basically just removed the if exit == 0: from oprypin's code

@ghost
Copy link

ghost commented Apr 21, 2018

WARNING

DO NOT REMOVE that if exit == 0

I just saved, and randomly almost LOST my entire LoginHandler.cr file (it replaced my file with blank text). Doing a back up right now and going back to the original crystal_format.py with the cmd pop up for now. Not risking anything atm sorry

@faustinoaq
Copy link
Member Author

@girng No problem, keep trying, Thank you for be aware of this issue 👍

@ghost
Copy link

ghost commented Apr 21, 2018

Praise to you and opryprin for help. just need solution for global name 'exit' is not defined and this can finally be solved!!

@oprypin
Copy link
Contributor

oprypin commented Apr 21, 2018

Needs exit = proc.returncode

Crystal Tools automation moved this from To do to Done Jun 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Crystal Tools
  
Done
Development

No branches or pull requests

2 participants