Skip to content

Commit

Permalink
fix: init lsp with workspace folder
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanba committed Mar 26, 2024
1 parent 4312327 commit 71dd94b
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 18 deletions.
7 changes: 5 additions & 2 deletions packages/libro-core/src/command/document-commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ClearOutlined, SaveOutlined, SettingOutlined } from '@ant-design/icons';
import { SaveOutlined, SettingOutlined } from '@ant-design/icons';
import type { Command } from '@difizen/mana-app';

import { FormatCellIcon } from '../material-from-designer.js';

export const DocumentCommands: Record<string, Command & { keybind?: string }> = {
Save: {
id: 'document:save',
Expand All @@ -15,7 +17,8 @@ export const DocumentCommands: Record<string, Command & { keybind?: string }> =
},
FormatCell: {
id: 'document.notebook.format_cell',
icon: ClearOutlined,
icon: FormatCellIcon,
label: 'format cell code',
keybind: 'shift+alt+f',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ export class LibroCommandContribution implements CommandContribution {
return false;
}
return (
this.libroService.hasFormatter &&
!libro?.model.readOnly &&
EditorCellView.is(cell) &&
cell.model.mimeType === MIME.python &&
Expand Down
4 changes: 4 additions & 0 deletions packages/libro-core/src/libro-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class LibroService implements NotebookService, Disposable {
protected themeService: ThemeService;
@prop()
themeMode: string;

@prop()
hasFormatter = false;

constructor(@inject(ThemeService) themeService: ThemeService) {
this.themeService = themeService;
this.themeMode = this.themeService.getCurrentTheme().type;
Expand Down
11 changes: 11 additions & 0 deletions packages/libro-core/src/libro-setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,14 @@ export const OutputScrollBtnVisiable: ConfigurationNode<boolean> = {
type: 'boolean',
},
};

export const SupportCodeFormat: ConfigurationNode<boolean> = {
id: 'libro.language.codeformat',
description: l10n.t('是否支持代码格式化'),
title: l10n.t('是否支持代码格式化'),
type: 'checkbox',
defaultValue: false,
schema: {
type: 'boolean',
},
};
6 changes: 5 additions & 1 deletion packages/libro-core/src/libro-workspace-service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { URI } from '@difizen/mana-app';
import { Deferred, URI } from '@difizen/mana-app';
import { singleton } from '@difizen/mana-app';

import type { NotebookView } from './libro-protocol.js';

export const ILibroWorkspaceService = Symbol('ILibroWorkspaceService');
export interface ILibroWorkspaceService {
ready: Promise<void>;
get workspaceRoot(): URI;
get notebooks(): NotebookView[];
get files(): URI[];
}

@singleton({ contrib: ILibroWorkspaceService })
export class BaseWorkspaceService implements ILibroWorkspaceService {
protected deferred = new Deferred<void>();
ready = this.deferred.promise;

get workspaceRoot() {
return new URI('/');
}
Expand Down
30 changes: 30 additions & 0 deletions packages/libro-core/src/material-from-designer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,33 @@ export const DisableOutputScroll: React.FC = () => (
</g>
</svg>
);

export const FormatCellIcon: React.FC = () => {
return (
<svg width="16px" height="16px" viewBox="0 0 16 16" version="1.1">
<g id="页面-1" stroke="none" strokeWidth="1" fill="none" fillRule="evenodd">
<g id="file" fill="#000000">
<rect
id="矩形"
fillRule="nonzero"
opacity="0"
x="0"
y="0"
width="16"
height="16"
></rect>
<path
d="M13.353125,4.509375 L9.990625,1.146875 C9.896875,1.053125 9.7703125,1 9.6375,1 L3,1 C2.7234375,1 2.5,1.2234375 2.5,1.5 L2.5,14.5 C2.5,14.7765625 2.7234375,15 3,15 L13,15 C13.2765625,15 13.5,14.7765625 13.5,14.5 L13.5,4.8640625 C13.5,4.73125 13.446875,4.603125 13.353125,4.509375 Z M12.346875,5.09375 L9.40625,5.09375 L9.40625,2.153125 L12.346875,5.09375 Z M12.375,13.875 L3.625,13.875 L3.625,2.125 L8.34375,2.125 L8.34375,5.5 C8.34375,5.8625 8.6375,6.15625 9,6.15625 L12.375,6.15625 L12.375,13.875 Z"
id="形状"
fillRule="nonzero"
></path>
<path
d="M9.37349398,7 C9.47995851,7 9.56626506,7.08630655 9.56626506,7.19277108 L9.56626506,12.2048193 C9.56626506,12.3112838 9.47995851,12.3975904 9.37349398,12.3975904 L6.19277108,12.3975904 C6.08630655,12.3975904 6,12.3112838 6,12.2048193 L6,7.19277108 C6,7.08630655 6.08630655,7 6.19277108,7 L9.37349398,7 Z M8.81980094,7.74698795 L6.79570456,7.74698795 C6.68924003,7.74698795 6.60293347,7.83329451 6.60293347,7.93975904 L6.60293347,9.86746988 C6.60293347,9.97393441 6.68924003,10.060241 6.79570456,10.060241 L8.81980094,10.060241 C8.92626547,10.060241 9.01257203,9.97393441 9.01257203,9.86746988 L9.01257203,7.93975904 C9.01257203,7.83329451 8.92626547,7.74698795 8.81980094,7.74698795 Z"
id="形状结合"
transform="translate(7.783133, 9.698795) scale(1, -1) rotate(45.000000) translate(-7.783133, -9.698795) "
></path>
</g>
</g>
</svg>
);
};
26 changes: 13 additions & 13 deletions packages/libro-core/src/toolbar/libro-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ export class LibroToolbarContribution implements ToolbarContribution {
group: ['group2'],
order: 'c-all',
});
// registry.registerItem({
// id: DocumentCommands['FormatCell'].id,
// command: DocumentCommands['FormatCell'].id,
// tooltip: (
// <div className="libro-side-tooltip">
// <span className="libro-tooltip-text">{l10n.t('格式化代码')}</span>
// <span className="libro-tooltip-keybind">Shift+Option+F</span>
// </div>
// ),

// group: ['sidetoolbar1'],
// order: 'd',
// });
registry.registerItem({
id: DocumentCommands['FormatCell'].id,
command: DocumentCommands['FormatCell'].id,
tooltip: (
<div className="libro-side-tooltip">
<span className="libro-tooltip-text">{l10n.t('格式化代码')}</span>
<span className="libro-tooltip-keybind">Shift+Option+F</span>
</div>
),

group: ['sidetoolbar1'],
order: 'd',
});
registry.registerItem({
id: NotebookCommands['MoveCellUp'].id,
command: NotebookCommands['MoveCellUp'].id,
Expand Down
47 changes: 47 additions & 0 deletions packages/libro-jupyter/src/libro-jupyter-workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { URL } from '@difizen/libro-common';
import { BaseWorkspaceService, ILibroWorkspaceService } from '@difizen/libro-core';
import { ServerConnection, ServerManager } from '@difizen/libro-kernel';
import { ApplicationContribution } from '@difizen/mana-app';
import { URI } from '@difizen/mana-app';
import { inject, singleton } from '@difizen/mana-app';

interface JupyterWorkspaceData {
rootUri: string;
}

@singleton({ contrib: [ILibroWorkspaceService, ApplicationContribution] })
export class JupyterWorkspaceService
extends BaseWorkspaceService
implements ILibroWorkspaceService, ApplicationContribution
{
@inject(ServerConnection) serverConnection: ServerConnection;
@inject(ServerManager) serverManager: ServerManager;

protected workspaceData: JupyterWorkspaceData;

onViewStart() {
const settings = { ...this.serverConnection.settings };
const url = URL.join(settings.baseUrl, '/libro/api/workspace');
this.serverManager.ready
.then(() => this.serverConnection.makeRequest(url, {}))
.then(async (res) => {
const data = await res.json();
this.workspaceData = data;
this.deferred.resolve();
return;
})
.catch(() => {
return;
});
}

override get workspaceRoot() {
return new URI(this.workspaceData.rootUri);
}
override get notebooks() {
return [];
}
override get files() {
return [];
}
}
2 changes: 2 additions & 0 deletions packages/libro-jupyter/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { LibroJupyterModel } from './libro-jupyter-model.js';
import { KernelStatusAndSelectorProvider } from './libro-jupyter-protocol.js';
import { JupyterServerLaunchManager } from './libro-jupyter-server-launch-manager.js';
import { LibroJupyterView } from './libro-jupyter-view.js';
import { JupyterWorkspaceService } from './libro-jupyter-workspace.js';
import { LibroJupyterOutputArea } from './output/index.js';
import { PlotlyModule } from './rendermime/index.js';
import { LibroJupyterColorContribution } from './theme/index.js';
Expand All @@ -57,6 +58,7 @@ import {

export const LibroJupyterModule = ManaModule.create()
.register(
JupyterWorkspaceService,
LibroJupyterFileService,
LibroJupyterCommandContribution,
LibroJupyterKeybindingContribution,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { EditorCellView, LibroService } from '@difizen/libro-core';
import {
EditorCellView,
ILibroWorkspaceService,
LibroService,
} from '@difizen/libro-core';
import { ExecutableNotebookModel } from '@difizen/libro-kernel';
import { ApplicationContribution, inject, singleton } from '@difizen/mana-app';
import * as monaco from '@difizen/monaco-editor-core';
Expand All @@ -12,6 +16,8 @@ import { getCellURI, toEditorRange, toMonacoPosition } from './util.js';

@singleton({ contrib: [ApplicationContribution] })
export class LibroLanguageClientContribution implements ApplicationContribution {
@inject(ILibroWorkspaceService)
protected readonly libroWorkspaceService: ILibroWorkspaceService;
@inject(LibroLanguageClientManager)
protected readonly libroLanguageClientManager: LibroLanguageClientManager;

Expand All @@ -28,6 +34,8 @@ export class LibroLanguageClientContribution implements ApplicationContribution
}

async startLanguageClients() {
// wait for workspaceFolder
await this.libroWorkspaceService.ready;
await this.lspEnv.ready;
const serverIds = await this.libroLanguageClientManager.getServers();

Expand All @@ -48,6 +56,16 @@ export class LibroLanguageClientContribution implements ApplicationContribution
},
});
}

this.libroLanguageClientManager
.getFeatureStatus('textDocument/formatting')
.then((status) => {
this.libroService.hasFormatter = status;
return;
})
.catch(() => {
return;
});
}

setupEditorOpener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export class LibroLanguageClientManager {

protected clientMap = new Map<string, IConnectionData>();

/**
* lsp server that support notebook
*/
protected supportedLSPServer: string[] = ['libro-analyzer', 'ruff-lsp', 'pylance'];

@postConstruct()
init() {
this.languageServerManager = this.languageServerManagerFactory({});
Expand All @@ -39,7 +44,7 @@ export class LibroLanguageClientManager {
const serverIds = this.languageServerManager.getMatchingServers({
language,
});
return serverIds;
return serverIds.filter((item) => this.supportedLSPServer.includes(item));
}

async getServerSpecs(language = 'python') {
Expand Down Expand Up @@ -72,6 +77,9 @@ export class LibroLanguageClientManager {
languageServerId: string,
options: LibroLanguageClientOptions,
) {
if (!this.supportedLSPServer.includes(languageServerId)) {
return;
}
if (this.clientMap.has(languageServerId)) {
return this.clientMap.get(languageServerId);
}
Expand Down

0 comments on commit 71dd94b

Please sign in to comment.