From 2f54c1d653d46831eeb7d691c5f25b78ca63378a Mon Sep 17 00:00:00 2001
From: Julien Roncaglia <julien@roncaglia.fr>
Date: Mon, 2 Mar 2020 22:54:29 +0100
Subject: Centralize the check for languageId on document

Also move visibleRustEditors to Ctx
---
 editors/code/src/commands/syntax_tree.ts |  5 +++--
 editors/code/src/ctx.ts                  |  9 ++++++++-
 editors/code/src/highlighting.ts         |  4 ++--
 editors/code/src/inlay_hints.ts          | 23 ++++-------------------
 editors/code/src/util.ts                 |  8 ++++++++
 5 files changed, 25 insertions(+), 24 deletions(-)

diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts
index 7218bfb90..2e08e8f11 100644
--- a/editors/code/src/commands/syntax_tree.ts
+++ b/editors/code/src/commands/syntax_tree.ts
@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
 import * as ra from '../rust-analyzer-api';
 
 import { Ctx, Cmd } from '../ctx';
+import { isRustDocument } from '../util';
 
 // Opens the virtual file that will show the syntax tree
 //
@@ -19,7 +20,7 @@ export function syntaxTree(ctx: Ctx): Cmd {
     vscode.workspace.onDidChangeTextDocument(
         (event: vscode.TextDocumentChangeEvent) => {
             const doc = event.document;
-            if (doc.languageId !== 'rust') return;
+            if (!isRustDocument(doc)) return;
             afterLs(() => tdcp.eventEmitter.fire(tdcp.uri));
         },
         null,
@@ -28,7 +29,7 @@ export function syntaxTree(ctx: Ctx): Cmd {
 
     vscode.window.onDidChangeActiveTextEditor(
         (editor: vscode.TextEditor | undefined) => {
-            if (!editor || editor.document.languageId !== 'rust') return;
+            if (!editor || !isRustDocument(editor.document)) return;
             tdcp.eventEmitter.fire(tdcp.uri);
         },
         null,
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index 43540e0d8..b4e983a0c 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -3,6 +3,7 @@ import * as lc from 'vscode-languageclient';
 
 import { Config } from './config';
 import { createClient } from './client';
+import { isRustDocument } from './util';
 
 export class Ctx {
     private constructor(
@@ -23,11 +24,17 @@ export class Ctx {
 
     get activeRustEditor(): vscode.TextEditor | undefined {
         const editor = vscode.window.activeTextEditor;
-        return editor && editor.document.languageId === 'rust'
+        return editor && isRustDocument(editor.document)
             ? editor
             : undefined;
     }
 
+    get visibleRustEditors(): vscode.TextEditor[] {
+        return vscode.window.visibleTextEditors.filter(
+            editor => isRustDocument(editor.document),
+        );
+    }
+
     registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
         const fullName = `rust-analyzer.${name}`;
         const cmd = factory(this);
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts
index 3e0cbdc56..036183834 100644
--- a/editors/code/src/highlighting.ts
+++ b/editors/code/src/highlighting.ts
@@ -4,7 +4,7 @@ import * as ra from './rust-analyzer-api';
 import { ColorTheme, TextMateRuleSettings } from './color_theme';
 
 import { Ctx } from './ctx';
-import { sendRequestWithRetry } from './util';
+import { sendRequestWithRetry, isRustDocument } from './util';
 
 export function activateHighlighting(ctx: Ctx) {
     const highlighter = new Highlighter(ctx);
@@ -36,7 +36,7 @@ export function activateHighlighting(ctx: Ctx) {
 
     vscode.window.onDidChangeActiveTextEditor(
         async (editor: vscode.TextEditor | undefined) => {
-            if (!editor || editor.document.languageId !== 'rust') return;
+            if (!editor || !isRustDocument(editor.document)) return;
             if (!ctx.config.highlightingOn) return;
             const client = ctx.client;
             if (!client) return;
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts
index 46e5f7c0d..08d3a64a7 100644
--- a/editors/code/src/inlay_hints.ts
+++ b/editors/code/src/inlay_hints.ts
@@ -2,9 +2,7 @@ import * as vscode from 'vscode';
 import * as ra from './rust-analyzer-api';
 
 import { Ctx } from './ctx';
-import { log, sendRequestWithRetry } from './util';
-
-const noInlayUriSchemes = ['git', 'svn'];
+import { log, sendRequestWithRetry, isRustDocument } from './util';
 
 export function activateInlayHints(ctx: Ctx) {
     const hintsUpdater = new HintsUpdater(ctx);
@@ -17,7 +15,7 @@ export function activateInlayHints(ctx: Ctx) {
     vscode.workspace.onDidChangeTextDocument(
         async event => {
             if (event.contentChanges.length === 0) return;
-            if (event.document.languageId !== 'rust') return;
+            if (!isRustDocument(event.document)) return;
             await hintsUpdater.refresh();
         },
         null,
@@ -79,7 +77,7 @@ class HintsUpdater {
     }
 
     clear() {
-        this.allEditors.forEach(it => {
+        this.ctx.visibleRustEditors.forEach(it => {
             this.setTypeDecorations(it, []);
             this.setParameterDecorations(it, []);
         });
@@ -87,20 +85,7 @@ class HintsUpdater {
 
     async refresh() {
         if (!this.enabled) return;
-        await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
-    }
-
-    private get allEditors(): vscode.TextEditor[] {
-        return vscode.window.visibleTextEditors.filter(
-            editor => {
-                if (editor.document.languageId !== 'rust') {
-                    return false;
-                }
-                const scheme = editor.document.uri.scheme;
-                const hasBlacklistedScheme = noInlayUriSchemes.some(s => s === scheme);
-                return !hasBlacklistedScheme;
-            },
-        );
+        await Promise.all(this.ctx.visibleRustEditors.map(it => this.refreshEditor(it)));
     }
 
     private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts
index f56c6bada..7c95769bb 100644
--- a/editors/code/src/util.ts
+++ b/editors/code/src/util.ts
@@ -1,6 +1,7 @@
 import * as lc from "vscode-languageclient";
 import * as vscode from "vscode";
 import { strict as nativeAssert } from "assert";
+import { TextDocument } from "vscode";
 
 export function assert(condition: boolean, explanation: string): asserts condition {
     try {
@@ -65,3 +66,10 @@ export async function sendRequestWithRetry<TParam, TRet>(
 function sleep(ms: number) {
     return new Promise(resolve => setTimeout(resolve, ms));
 }
+
+export function isRustDocument(document: TextDocument) {
+    return document.languageId === 'rust'
+        // SCM diff views have the same URI as the on-disk document but not the same content
+        && document.uri.scheme !== 'git'
+        && document.uri.scheme !== 'svn';
+}
\ No newline at end of file
-- 
cgit v1.2.3