diff --git a/projectsystem/DocumentMapping.py b/projectsystem/DocumentMapping.py index 830ddb6..ca53111 100644 --- a/projectsystem/DocumentMapping.py +++ b/projectsystem/DocumentMapping.py @@ -1,5 +1,9 @@ +import os +import logging from projectsystem import Sourcemap +logger = logging.getLogger("SWI") + class Position: """ All lines and columns in Sublime, in source maps, and in the WebKit protocol are explicitly specified to be @@ -89,6 +93,7 @@ class MappingInfo: line_mappings = [] def __init__(self, generated_file): + source_map_file = Sourcemap.get_sourcemap_file(generated_file) if not len(source_map_file): return @@ -97,8 +102,13 @@ def __init__(self, generated_file): if not self.parsed_source_map.is_valid(): return + logger.info(' Source map is valid') + self.generated_file = generated_file self.authored_sources = self.parsed_source_map.get_authored_sources_path() + + [logger.info(' Found authored source %s which exists? %s' % (s, str(os.path.isfile(s)))) for s in self.authored_sources] + self.line_mappings = self.parsed_source_map.line_mappings def is_valid(self): diff --git a/projectsystem/Sourcemap.py b/projectsystem/Sourcemap.py index 9edadc1..2320ff6 100644 --- a/projectsystem/Sourcemap.py +++ b/projectsystem/Sourcemap.py @@ -1,20 +1,30 @@ import json import os +import logging from projectsystem import VLQDecoder +logger = logging.getLogger("SWI") + def get_sourcemap_file(file_name): sourcemap_prefix = "//# sourceMappingURL=" map_file = "" + + logger.info(' Looking for source map in %s' % (file_name)) + try: - with open(file_name, "r") as f: + with open(file_name, "r", encoding="utf8") as f: # Read the last line of the file containing sourcemap information sourcemap_info = f.readlines()[-1] - if (sourcemap_info and len(sourcemap_info) > 0 and sourcemap_info.index(sourcemap_prefix) is 0): - map_file = sourcemap_info[len(sourcemap_prefix):].strip() - map_file = os.path.dirname(file_name) + os.path.sep + map_file + if sourcemap_info and len(sourcemap_info) > 0: + sourcemap_info.strip() + index = sourcemap_info.find(sourcemap_prefix) + if index == 0: + map_file = sourcemap_info[len(sourcemap_prefix):] + map_file = os.path.dirname(file_name) + os.path.sep + map_file + logger.info(' Found sourcemap comment %s' % (sourcemap_info)) f.close() except Exception as e: - print('Could not read %s: %s' % (file_name, str(e))) + logger.info(' Could not read %s for source map info: %s' % (file_name, str(e))) pass return map_file @@ -22,9 +32,14 @@ def get_sourcemap_file(file_name): class ParsedSourceMap: def __init__(self, file_name): + self.authored_sources = None + self.line_mappings = None + + logger.info(' Reading source map %s' % (file_name)) + try: self.content = None - with open(file_name, "r") as f: + with open(file_name, "r", encoding="utf8") as f: self.content = json.loads(f.read()) f.close() @@ -34,7 +49,7 @@ def __init__(self, file_name): self.authored_sources = self.content["sources"] self.line_mappings = SourceMapParser.calculate_line_mappings(self.content) except Exception as e: - print('Could not read %s: %s' % (file_name, str(e))) + logger.info(' Could not read source map %s: %s' % (file_name, str(e))) pass def is_valid(self): diff --git a/protocol.py b/protocol.py index 33418d8..c712e48 100644 --- a/protocol.py +++ b/protocol.py @@ -47,7 +47,7 @@ def send(self, command, callback=None, options=None): command.options = options self.commands[command.id] = command self.next_id += 1 - if utils.get_setting('debug_mode'): + if utils.get_setting('ws_debug_mode'): print ('SWI: ->> ' + json.dumps(command.request, sort_keys=True, indent=4, separators=(',', ': '))) self.socket.send(json.dumps(command.request)) @@ -65,7 +65,7 @@ def message_callback(self, ws, message): Parse it and call matching callback. """ parsed = json.loads(message) - if utils.get_setting('debug_mode'): + if utils.get_setting('ws_debug_mode'): print ('SWI: <<- ' + json.dumps(parsed, sort_keys=True, indent=4, separators=(',', ': '))) if 'method' in parsed: if parsed['method'] in self.notifications: diff --git a/swi.py b/swi.py index 937ea71..a50d52e 100644 --- a/swi.py +++ b/swi.py @@ -13,6 +13,7 @@ import sys import imp import re +import logging swi_folder = os.path.dirname(os.path.realpath(__file__)) if not swi_folder in sys.path: @@ -56,8 +57,15 @@ breakpoint_inactive_icon = 'Packages/Web Inspector/icons/breakpoint_inactive.png' breakpoint_current_icon = 'Packages/Web Inspector/icons/breakpoint_current.png' +logger = logging.getLogger("SWI") +logger.propagate = False + def plugin_loaded(): + if not logger.handlers and utils.get_setting('debug_mode'): + logger.addHandler(logging.StreamHandler()) + logger.setLevel(logging.INFO) + close_all_our_windows() clear_all_views() @@ -281,6 +289,8 @@ def scriptParsed(self, data, notification): file_name = '' script = get_script(data['url']) + logger.info('====Notified of url %s====' % url) + if script: if int(scriptId) > int(script['scriptId']): script['scriptId'] = str(scriptId) @@ -296,22 +306,30 @@ def scriptParsed(self, data, notification): # eg., folder is c:\site and url is http://localhost/app.js # glob for c:\site\app.js (primary) and c:\site\*\app.js (fallback only - there may be a c:\site\foo\app.js) try: - files = glob.glob(folder + "\\" + "\\".join(url_parts)) + glob.glob(folder + "\\*\\" + "\\".join(url_parts)) + glob1 = folder + "\\" + "\\".join(url_parts) + glob2 = folder + "\\*\\" + "\\".join(url_parts) + logger.info(' Glob for files at %s and %s' % (glob1, glob2)) + files = glob.glob(glob1) + glob.glob(glob2) except: pass else: - files = glob.glob(folder + "/" + "/".join(url_parts)) + glob.glob(folder + "/*/" + "/".join(url_parts)) + glob1 = folder + "/" + "/".join(url_parts) + glob2 = folder + "/*/" + "/".join(url_parts) + logger.info(' Glob for files at %s and %s' % (glob1, glob2)) + files = glob.glob(glob1) + glob.glob(glob2) if len(files) > 0 and files[0] != '': file_name = files[0] if (file_name): + logger.info(' Matched %s to %s' % (url, file_name)) # Create a file mapping to look for mapped source code projectsystem.DocumentMapping.MappingsManager.create_mapping(file_name) - file_to_scriptId.append({'file': file_name, 'scriptId': str(scriptId), 'url': data['url']}) + file_to_scriptId.append({'file': file_name, 'scriptId': str(scriptId), 'url': data['url']}) # don't try to match shorter fragments, we already found a match url_parts = [] + if len(url_parts) > 0: del url_parts[0]