Skip to content

Commit

Permalink
Finish syntax highlighting port
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Jul 8, 2023
1 parent da7ab8f commit 78b3e70
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
3 changes: 1 addition & 2 deletions packages/jupyterlab-lsp/src/features/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export class SignatureFeature extends Feature {
let language = 'python';
let markup = this.get_markup_for_signature_help(response, language);

this.console.log(
this.console.debug(
'Signature will be shown',
language,
markup,
Expand Down Expand Up @@ -552,7 +552,6 @@ export class SignatureFeature extends Feature {
editorPosition: IEditorPosition
) {
const lastCharacter = extractLastCharacter(change);
console.log('lastCharacter', lastCharacter);

const isSignatureShown = this.isSignatureShown();
let previousPosition: IEditorPosition | null = null;
Expand Down
34 changes: 19 additions & 15 deletions packages/jupyterlab-lsp/src/features/syntax_highlighting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
IEditorMimeTypeService,
IEditorServices
} from '@jupyterlab/codeeditor';
import { CodeMirrorEditor ,
import {
CodeMirrorEditor,
IEditorExtensionRegistry,
IEditorLanguageRegistry,
EditorExtensionRegistry
Expand Down Expand Up @@ -42,7 +43,7 @@ export class SyntaxHighlightingFeature extends Feature {
const connectionManager = options.connectionManager;

options.editorExtensionRegistry.addExtension({
name: 'lsp:codeSignature',
name: 'lsp:syntaxHighlighting',
factory: options => {
const updateListener = EditorView.updateListener.of(viewUpdate => {
if (!viewUpdate.docChanged) {
Expand All @@ -57,7 +58,7 @@ export class SyntaxHighlightingFeature extends Feature {
// const editor = adapter.editors.find(e => e.model === options.model);

if (adapter) {
this.update_mode(adapter, viewUpdate.view);
this.updateMode(adapter, viewUpdate.view).catch(console.warn);
}
});

Expand All @@ -69,7 +70,7 @@ export class SyntaxHighlightingFeature extends Feature {
}

private getMode(language: string): string | undefined {
let mimetype = this.options.mimeTypeService.getMimeTypeByLanguage({
const mimetype = this.options.mimeTypeService.getMimeTypeByLanguage({
name: language
});

Expand All @@ -89,8 +90,8 @@ export class SyntaxHighlightingFeature extends Feature {
return mimetype;
}

update_mode(adapter: WidgetLSPAdapter<any>, view: EditorView) {
let topDocument = adapter.virtualDocument as VirtualDocument;
async updateMode(adapter: WidgetLSPAdapter<any>, view: EditorView) {
const topDocument = adapter.virtualDocument as VirtualDocument;
const totalArea = view.state.doc.length;

// TODO no way to map from EditorView to Document.IEditor is blocking here.
Expand All @@ -108,17 +109,18 @@ export class SyntaxHighlightingFeature extends Feature {
if (!topDocument) {
return;
}
await topDocument.updateManager.updateDone;

const overrides = new Map();
for (let map of topDocument.getForeignDocuments(editorAccessor)) {
for (let [range, block] of map.entries()) {
let editor = block.editor.getEditor()! as CodeMirrorEditor;
let covered_area =
for (const map of topDocument.getForeignDocuments(editorAccessor)) {
for (const [range, block] of map.entries()) {
const editor = block.editor.getEditor()! as CodeMirrorEditor;
const coveredArea =
editor.getOffsetAt(range.end) - editor.getOffsetAt(range.start);
let coverage = covered_area / totalArea;
const coverage = coveredArea / totalArea;

let language = block.virtualDocument.language;
let mode = this.getMode(language);
const language = block.virtualDocument.language;
const mode = this.getMode(language);

// if not highlighting mode available, skip this editor
if (typeof mode === 'undefined') {
Expand All @@ -127,10 +129,11 @@ export class SyntaxHighlightingFeature extends Feature {

// change the mode if the majority of the code is the foreign code
if (coverage > this.options.settings.composite.foreignCodeThreshold) {
const original = editor.model.mimeType;
// this will trigger a side effect of switching language by updating
// private language compartment (implementation detail).
editor.model.mimeType = mode;
overrides.set(editor, editor.model.mimeType);
overrides.set(editor, original);
}
}
}
Expand Down Expand Up @@ -175,7 +178,8 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
IEditorServices,
ISettingRegistry,
IEditorExtensionRegistry,
IEditorLanguageRegistry
IEditorLanguageRegistry,
ILSPDocumentConnectionManager
],
autoStart: true,
activate: async (
Expand Down
7 changes: 3 additions & 4 deletions packages/jupyterlab-lsp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ import { HOVER_PLUGIN } from './features/hover';
import { JUMP_PLUGIN } from './features/jump_to';
import { RENAME_PLUGIN } from './features/rename';
import { SIGNATURE_PLUGIN } from './features/signature';
//import { SYNTAX_HIGHLIGHTING_PLUGIN } from './features/syntax_highlighting';

import { SYNTAX_HIGHLIGHTING_PLUGIN } from './features/syntax_highlighting';
import { CODE_OVERRIDES_MANAGER } from './overrides';
import { SettingsUIManager, SettingsSchemaManager } from './settings';
import {
Expand Down Expand Up @@ -207,8 +206,8 @@ const default_features: JupyterFrontEndPlugin<void>[] = [
HOVER_PLUGIN,
RENAME_PLUGIN,
HIGHLIGHTS_PLUGIN,
DIAGNOSTICS_PLUGIN
//SYNTAX_HIGHLIGHTING_PLUGIN
DIAGNOSTICS_PLUGIN,
SYNTAX_HIGHLIGHTING_PLUGIN
];
const plugins: JupyterFrontEndPlugin<any>[] = [
LOG_CONSOLE,
Expand Down

0 comments on commit 78b3e70

Please sign in to comment.