Skip to content
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

feature: apply styles to modified preview lines #49

Merged
merged 1 commit into from
Jun 9, 2021
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
44 changes: 41 additions & 3 deletions src/preview/CodeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,37 @@ export class CodeProvider implements vscode.TextDocumentContentProvider {

public _isOpen = false;
private _timer: NodeJS.Timer | undefined = undefined;
private _editor: vscode.TextEditor | null = null;

readonly defaultLanguage: CodeProviderLanguage = 'json';

constructor(public _document: vscode.TextDocument, public options: CodeProviderOptions) {
this.scheme = `expo-config-${this.options.type}`;
this.projectRoot = getProjectRoot(this._document);

this._changeSubscription = vscode.workspace.onDidChangeTextDocument((ev) =>
this.textDidChange(ev)
);
this._changeSubscription = vscode.workspace.onDidChangeTextDocument((ev) => {
this.textDidChange(ev);
// Apply highlight to changed area
if (this._editor && ev.document === this._editor.document) {
const styles: vscode.DecorationOptions[] = [];
for (const change of ev.contentChanges) {
// Only style added lines
if (!change.text || !change.range) {
continue;
}
// Get the range of the modification
const range = new vscode.Range(
this._editor.document.positionAt(change.rangeOffset),
this._editor.document.positionAt(change.rangeOffset + change.text.length)
);
styles.push({
range,
});
}
// Apply decorations (styles)
this._editor.setDecorations(addedCodeStyle, styles);
}
});
this._onDidChangeVisibleTextEditors = vscode.window.onDidChangeVisibleTextEditors((editors) =>
this.visibleTextEditorsDidChange(editors)
);
Expand All @@ -48,6 +69,10 @@ export class CodeProvider implements vscode.TextDocumentContentProvider {
);
}

setEditor(editor: vscode.TextEditor) {
this._editor = editor;
}

getDefinedLanguage() {
return this.options.convertLanguage || this.defaultLanguage;
}
Expand Down Expand Up @@ -157,6 +182,7 @@ export class CodeProvider implements vscode.TextDocumentContentProvider {
this.fileContents = '';
vscode.window.showErrorMessage(error.message);
}

this.sendDidChangeEvent();
}

Expand All @@ -175,3 +201,15 @@ export class CodeProvider implements vscode.TextDocumentContentProvider {
return '';
}
}

// create a decorator type that we use to decorate small numbers
const addedCodeStyle = vscode.window.createTextEditorDecorationType({
// before: {
// contentText: '+',
// },
isWholeLine: true,
backgroundColor: '#e5ffec',
dark: {
backgroundColor: '#2ea04333',
},
});
4 changes: 3 additions & 1 deletion src/preview/setupPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,7 @@ async function openForEditor(

await codeProvider!.update();
const doc = await vscode.workspace.openTextDocument(codeProvider!.getURI());
vscode.window.showTextDocument(doc, column, true);
const editor = await vscode.window.showTextDocument(doc, column, true);

codeProvider.setEditor(editor);
}