Skip to content

Remove inlay hint in diff views #3388

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

Merged
merged 2 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions editors/code/src/commands/syntax_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions editors/code/src/highlighting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 4 additions & 10 deletions editors/code/src/inlay_hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode';
import * as ra from './rust-analyzer-api';

import { Ctx } from './ctx';
import { log, sendRequestWithRetry } from './util';
import { log, sendRequestWithRetry, isRustDocument } from './util';

export function activateInlayHints(ctx: Ctx) {
const hintsUpdater = new HintsUpdater(ctx);
Expand All @@ -15,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,
Expand Down Expand Up @@ -77,21 +77,15 @@ class HintsUpdater {
}

clear() {
this.allEditors.forEach(it => {
this.ctx.visibleRustEditors.forEach(it => {
this.setTypeDecorations(it, []);
this.setParameterDecorations(it, []);
});
}

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 => editor.document.languageId === 'rust',
);
await Promise.all(this.ctx.visibleRustEditors.map(it => this.refreshEditor(it)));
}

private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
Expand Down
8 changes: 8 additions & 0 deletions editors/code/src/util.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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';
}