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

CommandInputHandler.initial_selection not being called for plugin #5972

Closed
plutonium-239 opened this issue May 11, 2023 · 13 comments
Closed

Comments

@plutonium-239
Copy link

Description of the bug

I have made a very simple "Duplicate File" plugin, which makes use of the CommandInputHandler and I want the text input to have the same functionality as the Rename File default plugin (Default/rename.py). I had copied over the code for the Input Handler, which does implement initial_selection as such:
(I added the print statement for debugging)

def initial_selection(self):
	print('initial_selection')
	name = os.path.splitext(self.file_name)[0]
	return [(0, len(name))]

This does work in the default rename plugin as only the file name is selected initially and not the extension, but it does not work in my plugin, and on further inspection, the function is not even called, as there was no print in the console.

I have looked around and cannot seem to find any differences in the Input Handler.
This seems like a bug, but I apologize if I am mistaken.

Steps to reproduce

Here is a dummy plugin that does nothing except for calling the input handler.

import os,sublime_plugin

class dummyCommand(sublime_plugin.TextCommand):
	def input(self, args):
		file_name = self.view.file_name()
		file_name = os.path.split(file_name)[1]
		return MyTextInputHandler(file_name)

	def run(self, edit, **args):
		print('run called')

class MyTextInputHandler(sublime_plugin.TextInputHandler):
	def __init__(self, file_name):
		self.file_name = file_name

	def name(self):
		return 'file_name'

	def initial_text(self):
		return self.file_name

	def initial_selection(self):
		print('initial_selection')
		name = os.path.splitext(self.file_name)[0]
		return [(0, len(name))]

To call this, here is a simple .sublime-commands file:

[
	{
		"caption": "Dummy command for testing",
		"command": "dummy"
	},
]

Expected behavior

I expect the initial_selection function to be called as it does in the default plugin.

Actual behavior

The initial_selection function is not called.

Sublime Text build number

4143

Operating system & version

Windows 10

(Linux) Desktop environment and/or window manager

No response

Additional information

No response

OpenGL context information

No response

@deathaxe
Copy link
Collaborator

Working fine here. (Also on ST4143 on Windows)

Note that InputHandler work only if a command is registered in a Default.sublime-commands file.

@plutonium-239
Copy link
Author

Hi @deathaxe, I tried your suggestion, changed the name of my commands file to Default, but it is still not being called.
The Default.sublime-commands doesn't need to be the actual sublime default file and can be a file in my package folder right?

@deathaxe
Copy link
Collaborator

A Default.sublime-commands located in your package folder is all you need.

I just created a Packages/DebugInputHandler folder, and created those two files with content pasted from your initial post.

Command is called, and initial_selection is logged in console. Working as expected.

grafik

Note: python modules (plugins) are loaded from package's root folder.

@plutonium-239
Copy link
Author

I can confirm it does not work for me after making a new folder and following the same steps

@plutonium-239
Copy link
Author

@deathaxe Do you have any idea why it might not be working on my system?

@deathaxe
Copy link
Collaborator

You could try to add your example code to a ST instance running in SAFE MODE, to check whether other plugins etc. might be involved.

But otherwise I don't have any idea, as it is working fine on my box. Same OS, same ST build. Have also couple other plugins using that functionality.

@plutonium-239
Copy link
Author

Unfortunately, it does not work even in safe mode
image

@plutonium-239
Copy link
Author

Hey @deathaxe, sorry to bother you again, but just in case I did something wrong thrice, could you send your dummy package as a zip?
Thanks

@deathaxe
Copy link
Collaborator

I have nothing more to add here.

@BenjaminSchaaf
Copy link
Member

Following the steps given results in the plugin working as expected on my end too. Made a test.py in the User folder with the plugin and a User/Default.sublime-commands file with the command. Everything is working as expected here.

@jwortmann
Copy link

jwortmann commented Oct 25, 2023

I just opened #6175 with a description of the cause and with an easy fix in sublime_plugin.py. It works for TextInputHandler in Default/rename.py because the Default package runs on Python 3.8. The same applies for the User package.

@BenjaminSchaaf I can confirm this on ST4152, or maybe was it fixed in the meanwhile? For more context see my linked issue.

@BenjaminSchaaf
Copy link
Member

Ah yes, the difference is likely the plugin environment. As part of the fix for #6175 I've added that functionality for the 3.3 environment, so I'll leave this closed in favor of that issue.

@plutonium-239
Copy link
Author

Thank you @jwortmann for looking into that! I thought I was going insane

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants