Repository: sublimelinter/sublimelinter
Issue: Factor out "clear methods" in linter

We need to check whether the following methods:
- `clear_view`
- `clear_all` 
- `clear`
 which can be found in `sublime_linter` and `linter`, are still being used.
Both internally and by plugins.

A removal or refactoring may follow.

Changed files:
sublime_linter.py, lint/linter.py, lint/settings.py, highlight_view.py

diff --git a/highlight_view.py b/highlight_view.py
index beac4b4..9ae0ad1 100644
--- a/highlight_view.py
+++ b/highlight_view.py
@@ -42,13 +42,6 @@ def get_regions_keys(view):
     return set(view.settings().get(REGION_KEYS.format(view.id())) or [])
 
 
-def clear_view(view):
-    for key in get_regions_keys(view):
-        view.erase_regions(key)
-
-    remember_region_keys(view, [])
-
-
 def plugin_unloaded():
     events.off(on_lint_result)
 
diff --git a/lint/linter.py b/lint/linter.py
index cfc29f0..a009ed9 100644
--- a/lint/linter.py
+++ b/lint/linter.py
@@ -10,8 +10,7 @@ import shlex
 import sublime
 
 from . import persist, util
-from .. import highlight_view
-from .const import STATUS_KEY, WARNING, ERROR
+from .const import WARNING, ERROR
 
 ARG_RE = re.compile(r'(?P<prefix>@|--?)?(?P<name>[@\\w][\\w\\-]*)(?:(?P<joiner>[=:])(?:(?P<sep>.)(?P<multiple>\\+)?)?)?')
 NEAR_RE_TEMPLATE = r'(?<!"){}({}){}(?!")'
@@ -559,12 +558,12 @@ class Linter(metaclass=LinterMeta):
         vid = view.id()
         persist.views[vid] = view
         syntax = util.get_syntax(view)
+        persist.debug("detected syntax: {}".format(syntax))
 
-        if not syntax:
-            cls.remove(vid)
+        if not syntax:  # seems like a very rare, edge case
+            persist.view_linters.pop(vid, None)
             return
 
-        persist.debug("detected syntax: " + syntax)
         view_linters = persist.view_linters.get(vid, set())
         linters = set()
 
@@ -599,25 +598,6 @@ class Linter(metaclass=LinterMeta):
         elif reset and not linters and vid in persist.view_linters:
             del persist.view_linters[vid]
 
-    @classmethod
-    def remove(cls, vid):
-        """Remove a the mapping between a view and its set of linters."""
-        if vid in persist.view_linters:
-            for linters in persist.view_linters[vid]:
-                linters.clear()
-
-            del persist.view_linters[vid]
-
-    @classmethod
-    def clear_all(cls):
-        """Clear highlights and errors in all views."""
-        persist.errors.clear()
-
-    @classmethod
-    def text(cls, view):
-        """Return the entire text of a view."""
-        return view.substr(sublime.Region(0, view.size()))
-
     @classmethod
     def which(cls, cmd):
         """Return full path to a given executable.
@@ -1144,18 +1124,6 @@ class Linter(metaclass=LinterMeta):
 
         return text
 
-    @staticmethod
-    def clear_view(view):
-        """Clear marks, status and all other cached error info for the given view."""
-        if not view:
-            return
-
-        view.erase_status(STATUS_KEY)
-        highlight_view.clear_view(view)
-
-    def clear(self):
-        self.clear_view(self.view)
-
     # Helper methods
 
     @classmethod
diff --git a/lint/settings.py b/lint/settings.py
index 57b06be..85e411b 100644
--- a/lint/settings.py
+++ b/lint/settings.py
@@ -56,20 +56,12 @@ class Settings:
             return
 
         from . import style
-        from .linter import Linter
 
         # Reparse settings for style rules
         # Linter specific settings can include style rules too
         if self.has_changed('styles') or self.has_changed('linters'):
             style.StyleParser()()
 
-        # If the syntax map changed, reassign linters to all views
-        if self.has_changed('syntax_map'):
-            Linter.clear_all()
-            util.apply_to_all_views(
-                lambda view: Linter.assign(view, reset=True)
-            )
-
         if self.has_changed('gutter_theme'):
             style.load_gutter_icons()
 
diff --git a/sublime_linter.py b/sublime_linter.py
index 82c0454..4627c2a 100644
--- a/sublime_linter.py
+++ b/sublime_linter.py
@@ -272,11 +272,6 @@ class SublimeLinter(sublime_plugin.EventListener, Listener):
         self.check_syntax(view)
         self.linted_views.add(vid)
 
-        if view.size() == 0:
-            for linter in persist.view_linters.get(vid, []):
-                linter.clear()
-            return
-
         persist.last_hit_times[vid] = queue.hit(view)
 
     def check_syntax(self, view):
@@ -293,20 +288,26 @@ class SublimeLinter(sublime_plugin.EventListener, Listener):
 
         # Syntax either has never been set or just changed
         if vid not in self.view_syntax or self.view_syntax[vid] != syntax:
+            bid = view.buffer_id()
+            persist.errors[bid].clear()
+            for linter in persist.view_linters.get(vid, []):
+                events.broadcast(events.LINT_RESULT, {
+                    'buffer_id': bid,
+                    'linter_name': linter.name,
+                    'errors': []
+                })
+
             self.view_syntax[vid] = syntax
+            self.linted_views.discard(vid)
             Linter.assign(view, reset=True)
-            self.clear(view)
             return True
         else:
             return False
 
-    def clear(self, view):
-        Linter.clear_view(view)
-
     def view_has_file_only_linter(self, vid):
         """Return True if any linters for the given view are file-only."""
-        for lint in persist.view_linters.get(vid, []):
-            if lint.tempfile_suffix == '-':
+        for linter in persist.view_linters.get(vid, []):
+            if linter.tempfile_suffix == '-':
                 return True
 
         return False
@@ -422,13 +423,10 @@ class SublimeLinter(sublime_plugin.EventListener, Listener):
 
     def file_was_saved(self, view):
         """Check if the syntax changed or if we need to show errors."""
-        syntax_changed = self.check_syntax(view)
+        self.check_syntax(view)
         vid = view.id()
         mode = persist.settings.get('lint_mode')
 
-        if syntax_changed:
-            self.clear(view)
-
         if mode != 'manual':
             if vid in persist.view_linters or self.view_has_file_only_linter(vid):
                 self.hit(view)