Skip to content

Commit cabae5d

Browse files
added plugin version number on menu, try to fix "Auto completions slow and incorrect" #11, try to fix "Various errors in the command window" #12 on Windows
1 parent 1d29e45 commit cabae5d

File tree

16 files changed

+420
-105
lines changed

16 files changed

+420
-105
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ It offers better **javascript autocomplete** and also a lot of features about cr
1515
- Cordova projects (run cordova emulate, build, compile, serve, etc. directly from Sublime Text!)
1616
- Ionic v1 and v2 projects (same as Cordova projects!)
1717
- Angular v1 and v2 projects
18-
- React projects
19-
- Express projects
18+
- React projects (only about the creation for the moment)
19+
- React Native projects (only about the creation for the moment. I will add also **NativeScript** support)
20+
- Express projects (only about the creation for the moment)
2021
- Yeoman generators
2122
- Local bookmarks project
2223
- JavaScript real-time errors
23-
- etc. (I will add also **React Native** and **NativeScript** support)
24+
- etc.
2425

2526
You could use it also in **existing projects** (see the [Wiki](https://github.com/pichillilorenzo/JavaScriptEnhancements/wiki/Using-it-with-an-existing-project))!
2627

_generated_2018_01_09_at_16_37_39.py renamed to _generated_2018_01_10_at_20_34_18.py

Lines changed: 157 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from shutil import copyfile
44
from threading import Timer
55

6-
PLUGIN_VERSION = "0.11.1"
6+
PLUGIN_VERSION = "0.11.11"
77

88
PACKAGE_PATH = os.path.abspath(os.path.dirname(__file__))
99
PACKAGE_NAME = os.path.basename(PACKAGE_PATH)
@@ -28,7 +28,7 @@
2828
PLATFORM = platform_switcher.get(sublime.platform())
2929
PLATFORM_ARCHITECTURE = "64bit" if platform.architecture()[0] == "64bit" else "32bit"
3030

31-
PROJECT_TYPE_SUPPORTED = ['empty', 'angularv1', 'angularv2', 'cordova', 'express', 'ionicv1', 'ionicv2', 'react', 'yeoman']
31+
PROJECT_TYPE_SUPPORTED = ['empty', 'angularv1', 'angularv2', 'cordova', 'express', 'ionicv1', 'ionicv2', 'react', 'react-native', 'yeoman']
3232

3333
class Hook(object):
3434
hook_list = {}
@@ -1306,7 +1306,7 @@ def init(self):
13061306
mainPlugin = startPlugin()
13071307

13081308
import sublime, sublime_plugin
1309-
import os
1309+
import os, tempfile, queue
13101310
from collections import namedtuple
13111311

13121312
flowCLIRequirements = namedtuple('flowCLIRequirements', [
@@ -1316,20 +1316,29 @@ def init(self):
13161316
FLOW_DEFAULT_CONFIG_PATH = os.path.join(PACKAGE_PATH, "flow", ".flowconfig")
13171317

13181318
def find_flow_config(filename):
1319-
if not filename or filename is '/':
1320-
return FLOW_DEFAULT_CONFIG_PATH
13211319

1322-
potential_root = os.path.dirname(filename)
1323-
if os.path.isfile(os.path.join(potential_root, '.flowconfig')):
1324-
return potential_root
1320+
platform = sublime.platform()
1321+
1322+
while True:
1323+
1324+
if not filename:
1325+
return FLOW_DEFAULT_CONFIG_PATH
1326+
1327+
if platform == "windows" and len(filename) == 2 and filename[1] == ":":
1328+
return FLOW_DEFAULT_CONFIG_PATH
13251329

1326-
return find_flow_config(potential_root)
1330+
elif filename == '/':
1331+
return FLOW_DEFAULT_CONFIG_PATH
1332+
1333+
filename = os.path.dirname(filename)
1334+
if os.path.isfile(os.path.join(filename, '.flowconfig')):
1335+
return filename
13271336

13281337
def flow_parse_cli_dependencies(view, **kwargs):
13291338
filename = view.file_name()
13301339
contextual_keys = sublime.active_window().extract_variables()
13311340
folder_path = contextual_keys.get("folder")
1332-
if folder_path and os.path.isdir(folder_path) and os.path.isfile(os.path.join(folder_path, '.flowconfig')) :
1341+
if folder_path and os.path.isdir(folder_path) and os.path.isfile(os.path.join(folder_path, '.flowconfig')) :
13331342
project_root = folder_path
13341343
else :
13351344
project_root = find_flow_config(filename)
@@ -2660,6 +2669,77 @@ def react_prepare_project(project_path, react_custom_path):
26602669

26612670

26622671

2672+
import sublime, sublime_plugin
2673+
import os, webbrowser, shlex, json, collections
2674+
2675+
def react_native_ask_custom_path(project_path, type):
2676+
sublime.active_window().show_input_panel("Create-react-native-app CLI custom path", "create-react-native-app", lambda react_native_custom_path: react_native_prepare_project(project_path, react_native_custom_path) if type == "create_new_project" or type == "add_project_type" else add_react_native_settings(project_path, react_native_custom_path), None, None)
2677+
2678+
def add_react_native_settings(working_directory, react_native_custom_path):
2679+
project_path = working_directory
2680+
settings = get_project_settings()
2681+
if settings :
2682+
project_path = settings["project_dir_name"]
2683+
2684+
# flowconfig_file_path = os.path.join(project_path, ".flowconfig")
2685+
# with open(flowconfig_file_path, 'r+', encoding="utf-8") as file:
2686+
# content = file.read()
2687+
# content = content.replace("[ignore]", """[ignore]""")
2688+
# file.seek(0)
2689+
# file.truncate()
2690+
# file.write(content)
2691+
2692+
PROJECT_SETTINGS_FOLDER_PATH = os.path.join(project_path, PROJECT_SETTINGS_FOLDER_NAME)
2693+
2694+
default_config = json.loads(open(os.path.join(PROJECT_FOLDER, "react-native", "default_config.json")).read(), object_pairs_hook=collections.OrderedDict)
2695+
default_config["working_directory"] = working_directory
2696+
default_config["cli_custom_path"] = react_native_custom_path
2697+
2698+
react_native_settings = os.path.join(PROJECT_SETTINGS_FOLDER_PATH, "react_native_settings.json")
2699+
2700+
with open(react_native_settings, 'w+') as file:
2701+
file.write(json.dumps(default_config, indent=2))
2702+
2703+
def react_native_prepare_project(project_path, react_native_custom_path):
2704+
2705+
terminal = Terminal(cwd=project_path)
2706+
2707+
if sublime.platform() != "windows":
2708+
open_project = ["&&", shlex.quote(sublime_executable_path()), shlex.quote(get_project_settings(project_path)["project_file_name"])] if not is_project_open(get_project_settings(project_path)["project_file_name"]) else []
2709+
terminal.run([shlex.quote(react_native_custom_path), "myApp", ";", "mv", "./myApp/{.[!.],}*", "./", ";", "rm", "-rf", "myApp"] + open_project)
2710+
else:
2711+
open_project = [sublime_executable_path(), get_project_settings(project_path)["project_file_name"], "&&", "exit"] if not is_project_open(get_project_settings(project_path)["project_file_name"]) else []
2712+
terminal.run([react_native_custom_path, "myApp", "&", os.path.join(WINDOWS_BATCH_FOLDER, "move_all.bat"), "myApp", ".", "&", "rd", "/s", "/q", "myApp"])
2713+
if open_project:
2714+
terminal.run(open_project)
2715+
2716+
add_react_native_settings(project_path, react_native_custom_path)
2717+
2718+
Hook.add("react-native_after_create_new_project", react_native_ask_custom_path)
2719+
Hook.add("react-native_add_javascript_project_configuration", react_native_ask_custom_path)
2720+
Hook.add("react-native_add_javascript_project_type", react_native_ask_custom_path)
2721+
2722+
# class enable_menu_react_nativeEventListener(enable_menu_project_typeEventListener):
2723+
# project_type = "react-native"
2724+
# path = os.path.join(PROJECT_FOLDER, "react_native", "Main.sublime-menu")
2725+
# path_disabled = os.path.join(PROJECT_FOLDER, "react_native", "Main_disabled.sublime-menu")
2726+
2727+
# class react_native_cliCommand(manage_cliCommand):
2728+
2729+
# cli = "create-react-native-app"
2730+
# custom_name = "react_native"
2731+
# settings_name = "react_native_settings"
2732+
2733+
# def prepare_command(self, **kwargs):
2734+
2735+
# self._run()
2736+
2737+
# def _run(self):
2738+
2739+
# super(react_native_cliCommand, self)._run()
2740+
2741+
2742+
26632743
import sublime, sublime_plugin
26642744
import os, webbrowser, shlex, json
26652745

@@ -3489,6 +3569,8 @@ def create_completion(comp_name, comp_type, comp_details) :
34893569
class javascript_completionsEventListener(sublime_plugin.EventListener):
34903570
completions = None
34913571
completions_ready = False
3572+
searching = False
3573+
modified = False
34923574

34933575
# Used for async completions.
34943576
def run_auto_complete(self):
@@ -3523,20 +3605,23 @@ def on_query_completions(self, view, prefix, locations):
35233605
self.completions_ready = False
35243606
return self.completions
35253607

3526-
global javascript_completions_thread_list
3527-
3528-
javascript_completions_thread_list.append(Util.create_and_start_thread(target=lambda: self.on_query_completions_async(view, prefix, locations, len(javascript_completions_thread_list)+1), thread_name="JavaScriptEnhancementsCompletions"))
3608+
if not self.searching:
3609+
self.searching = True
3610+
self.modified = False
3611+
else:
3612+
return ([], sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
35293613

3530-
# sublime.set_timeout_async(
3531-
# lambda: self.on_query_completions_async(
3532-
# view, prefix, locations
3533-
# )
3534-
# )
3614+
sublime.set_timeout_async(
3615+
lambda: self.on_query_completions_async(
3616+
view, prefix, locations
3617+
)
3618+
)
35353619

35363620
if not self.completions_ready or not self.completions:
35373621
return ([], sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
35383622

3539-
def on_query_completions_async(self, view, prefix, locations, index_thread):
3623+
def on_query_completions_async(self, view, prefix, locations):
3624+
35403625
self.completions = None
35413626

35423627
if not view.match_selector(
@@ -3547,9 +3632,6 @@ def on_query_completions_async(self, view, prefix, locations, index_thread):
35473632

35483633
deps = flow_parse_cli_dependencies(view, add_magic_token=True, cursor_pos=locations[0])
35493634

3550-
if deps.project_root is '/':
3551-
return
3552-
35533635
flow_cli = "flow"
35543636
is_from_bin = True
35553637
chdir = ""
@@ -3564,6 +3646,10 @@ def on_query_completions_async(self, view, prefix, locations, index_thread):
35643646
chdir = settings["project_dir_name"]
35653647
use_node = False
35663648

3649+
if self.modified == True:
3650+
self.searching = False
3651+
return
3652+
35673653
node = NodeJS(check_local=True)
35683654

35693655
result = node.execute_check_output(
@@ -3585,6 +3671,11 @@ def on_query_completions_async(self, view, prefix, locations, index_thread):
35853671
)
35863672

35873673
if result[0]:
3674+
3675+
if self.modified == True:
3676+
self.searching = False
3677+
return
3678+
35883679
result = result[1]
35893680
self.completions = list()
35903681
for match in result['result'] :
@@ -3622,22 +3713,21 @@ def on_query_completions_async(self, view, prefix, locations, index_thread):
36223713
self.completions = (self.completions, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
36233714
self.completions_ready = True
36243715

3625-
sublime.active_window().active_view().run_command(
3626-
'hide_auto_complete'
3627-
)
3628-
36293716
view = sublime.active_window().active_view()
36303717
sel = view.sel()[0]
3631-
if view.substr(view.word(sel)).strip() :
36323718

3633-
global javascript_completions_thread_list
3719+
if view.substr(view.word(sel)).strip() :
36343720

3635-
if len(javascript_completions_thread_list) == 0 or index_thread+1 < len(javascript_completions_thread_list):
3721+
if self.modified == True:
3722+
self.searching = False
36363723
return
3637-
3724+
3725+
sublime.active_window().active_view().run_command(
3726+
'hide_auto_complete'
3727+
)
36383728
self.run_auto_complete()
3729+
self.searching = False
36393730

3640-
javascript_completions_thread_list = []
36413731

36423732
def on_text_command(self, view, command_name, args):
36433733
sel = view.sel()[0]
@@ -3648,6 +3738,8 @@ def on_text_command(self, view, command_name, args):
36483738
return
36493739

36503740
if command_name == "left_delete" :
3741+
self.modified = True
3742+
self.searching = False
36513743
scope = view.scope_name(view.sel()[0].begin()-1).strip()
36523744
if scope.endswith(" punctuation.accessor.js") or scope.endswith(" keyword.operator.accessor.js"):
36533745
sublime.active_window().active_view().run_command(
@@ -3675,14 +3767,17 @@ def on_selection_modified_async(self, view) :
36753767
locations = list()
36763768
locations.append(selections[0].begin())
36773769

3678-
global javascript_completions_thread_list
3770+
if not self.searching:
3771+
self.searching = True
3772+
self.modified = False
3773+
else:
3774+
return
36793775

3680-
javascript_completions_thread_list.append(Util.create_and_start_thread(target=lambda: self.on_query_completions_async(view, "", locations, len(javascript_completions_thread_list)+1), thread_name="JavaScriptEnhancementsCompletions"))
3681-
# sublime.set_timeout_async(
3682-
# lambda: self.on_query_completions_async(
3683-
# view, "", locations
3684-
# )
3685-
# )
3776+
sublime.set_timeout_async(
3777+
lambda: self.on_query_completions_async(
3778+
view, "", locations
3779+
)
3780+
)
36863781

36873782

36883783
import sublime, sublime_plugin
@@ -3853,6 +3948,26 @@ def description_details_html(description):
38533948
class on_hover_descriptionEventListener(sublime_plugin.EventListener):
38543949

38553950
def on_hover(self, view, point, hover_zone) :
3951+
if not view.match_selector(
3952+
point,
3953+
'source.js - comment'
3954+
):
3955+
return
3956+
3957+
if hover_zone != sublime.HOVER_TEXT :
3958+
return
3959+
3960+
for region in view.get_regions("flow_error"):
3961+
if region.contains(point):
3962+
return
3963+
3964+
region = view.word(point)
3965+
word = view.substr(region)
3966+
if not word.strip() :
3967+
return
3968+
3969+
view.hide_popup()
3970+
38563971
sublime.set_timeout_async(lambda: on_hover_description_async(view, point, hover_zone, point))
38573972

38583973
# used also by show_hint_parametersCommand
@@ -3875,14 +3990,11 @@ def on_hover_description_async(view, point, hover_zone, popup_position, show_hin
38753990
word = view.substr(region)
38763991
if not word.strip() :
38773992
return
3878-
3993+
38793994
cursor_pos = region.end()
38803995

38813996
deps = flow_parse_cli_dependencies(view, cursor_pos=cursor_pos, add_magic_token=True, not_add_last_part_tokenized_line=True)
38823997

3883-
if deps.project_root is '/':
3884-
return
3885-
38863998
flow_cli = "flow"
38873999
is_from_bin = True
38884000
chdir = ""
@@ -3957,8 +4069,7 @@ def on_hover_description_async(view, point, hover_zone, popup_position, show_hin
39574069

39584070
if not html :
39594071
deps = flow_parse_cli_dependencies(view)
3960-
if deps.project_root is '/':
3961-
return
4072+
39624073
row, col = view.rowcol(point)
39634074

39644075
flow_cli = "flow"
@@ -5148,6 +5259,8 @@ def donwload_can_i_use_json_data() :
51485259
if not json_file.closed:
51495260
json_file.close()
51505261
if os.path.isfile(path_to_test_can_i_use_data) :
5262+
if not json_file.closed:
5263+
json_file.close()
51515264
os.remove(path_to_test_can_i_use_data)
51525265
else :
51535266
os.rename(path_to_test_can_i_use_data, path_to_can_i_use_data)

0 commit comments

Comments
 (0)