-
Notifications
You must be signed in to change notification settings - Fork 137
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
Add load project logic at client side #1239
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import * as _ from 'lodash'; | ||
import { CancellationToken, TestController, TestItem, tests } from 'vscode'; | ||
import { isStandardServerReady } from '../extension'; | ||
import { loadJavaProjects } from './utils'; | ||
|
||
export let testController: TestController | undefined; | ||
|
||
export function createTestController(): void { | ||
if (!isStandardServerReady()) { | ||
return; | ||
} | ||
testController?.dispose(); | ||
testController = tests.createTestController('javaTestController', 'Java Test'); | ||
|
||
testController.resolveHandler = async (item: TestItem) => { | ||
await loadChildren(item); | ||
}; | ||
} | ||
|
||
export async function loadChildren(item: TestItem, token?: CancellationToken): Promise<void> { | ||
if (!item) { | ||
await loadJavaProjects(); | ||
return; | ||
} | ||
// todo: load other items | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import { TestItem } from 'vscode'; | ||
import { TestKind, TestLevel } from '../types'; | ||
|
||
/** | ||
* A map cache to save the metadata of the test item. | ||
* Use a WeakMap here so the key-value will be automatically collected when | ||
* the actual item is disposed. | ||
*/ | ||
class TestItemDataCache { | ||
private cache: WeakMap<TestItem, ITestItemData> = new WeakMap(); | ||
|
||
public set(item: TestItem, data: ITestItemData): void { | ||
this.cache.set(item, data); | ||
} | ||
|
||
public get(item: TestItem): ITestItemData | undefined { | ||
return this.cache.get(item); | ||
} | ||
|
||
public delete(item: TestItem): boolean { | ||
return this.cache.delete(item); | ||
} | ||
} | ||
|
||
export const dataCache: TestItemDataCache = new TestItemDataCache(); | ||
|
||
export interface ITestItemData { | ||
jdtHandler: string; | ||
fullName: string; | ||
projectName: string; | ||
testLevel: TestLevel; | ||
testKind: TestKind; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import * as _ from 'lodash'; | ||
import { CancellationToken, Range, TestItem, Uri, workspace, WorkspaceFolder } from 'vscode'; | ||
import { JavaTestRunnerDelegateCommands } from '../constants'; | ||
import { IJavaTestItem, TestLevel } from '../types'; | ||
import { executeJavaLanguageServerCommand } from '../utils/commandUtils'; | ||
import { testController } from './testController'; | ||
import { dataCache } from './testItemDataCache'; | ||
|
||
/** | ||
* Load the Java projects, which are the root nodes of the test explorer | ||
*/ | ||
export async function loadJavaProjects(): Promise<void> { | ||
for (const workspaceFolder of workspace.workspaceFolders || [] ) { | ||
const testProjects: IJavaTestItem[] = await getJavaProjects(workspaceFolder); | ||
for (const project of testProjects) { | ||
if (testController?.items.get(project.id)) { | ||
continue; | ||
} | ||
const projectItem: TestItem = createTestItem(project); | ||
projectItem.canResolveChildren = true; | ||
testController?.items.add(projectItem); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Create test item which will be shown in the test explorer | ||
* @param metaInfo The data from the server side of the test item | ||
* @param parent The parent node of the test item (if it has) | ||
* @returns The created test item | ||
*/ | ||
export function createTestItem(metaInfo: IJavaTestItem, parent?: TestItem): TestItem { | ||
if (!testController) { | ||
throw new Error('Failed to create test item. The test controller is not initialized.'); | ||
} | ||
const item: TestItem = testController.createTestItem( | ||
metaInfo.id, | ||
metaInfo.label, | ||
metaInfo.uri ? Uri.parse(metaInfo.uri) : undefined, | ||
); | ||
item.range = asRange(metaInfo.range); | ||
if (metaInfo.testLevel !== TestLevel.Invocation) { | ||
dataCache.set(item, { | ||
jdtHandler: metaInfo.jdtHandler, | ||
fullName: metaInfo.fullName, | ||
projectName: metaInfo.projectName, | ||
testLevel: metaInfo.testLevel, | ||
testKind: metaInfo.testKind, | ||
}); | ||
} | ||
if (parent) { | ||
parent.children.add(item); | ||
} | ||
return item; | ||
} | ||
|
||
/** | ||
* Parse the range object with server mode to client format | ||
* @param range | ||
*/ | ||
export function asRange(range: any): Range | undefined { | ||
if (!range) { | ||
return undefined; | ||
} | ||
return new Range(range.start.line, range.start.character, range.end.line, range.end.character); | ||
} | ||
|
||
export async function getJavaProjects(workspaceFolder: WorkspaceFolder, token?: CancellationToken): Promise<IJavaTestItem[]> { | ||
return await executeJavaLanguageServerCommand<IJavaTestItem[]>( | ||
JavaTestRunnerDelegateCommands.FIND_JAVA_PROJECTS, workspaceFolder.uri.toString(), token) || []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import { Range } from 'vscode'; | ||
|
||
/** | ||
* The test item data model that is returned from server side | ||
*/ | ||
export interface IJavaTestItem { | ||
children: IJavaTestItem[]; | ||
uri: string | undefined; | ||
range: Range | undefined; | ||
jdtHandler: string; | ||
fullName: string; | ||
label: string; | ||
id: string; | ||
projectName: string; | ||
testKind: TestKind; | ||
testLevel: TestLevel; | ||
} | ||
|
||
export enum TestKind { | ||
JUnit5 = 0, | ||
JUnit = 1, | ||
TestNG = 2, | ||
None = 100, | ||
} | ||
|
||
export enum TestLevel { | ||
Root = 0, | ||
Workspace = 1, | ||
WorkspaceFolder = 2, | ||
Project = 3, | ||
Package = 4, | ||
Class = 5, | ||
Method = 6, | ||
Invocation = 7, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it lsp2code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes